-
Notifications
You must be signed in to change notification settings - Fork 252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve parsing of author information #521
Open
yggi49
wants to merge
4
commits into
python-poetry:main
Choose a base branch
from
yggi49:improve-author-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
from poetry.core.packages.dependency_group import MAIN_GROUP | ||
from poetry.core.packages.specification import PackageSpecification | ||
from poetry.core.packages.utils.utils import create_nested_marker | ||
from poetry.core.utils.helpers import parse_author | ||
from poetry.core.version.exceptions import InvalidVersion | ||
from poetry.core.version.markers import parse_marker | ||
|
||
|
@@ -32,6 +33,8 @@ | |
|
||
T = TypeVar("T", bound="Package") | ||
|
||
# TODO: once poetry.console.commands.init.InitCommand._validate_author | ||
# uses poetry.core.utils.helpers.parse_author, this can be removed. | ||
AUTHOR_REGEX = re.compile(r"(?u)^(?P<name>[- .,\w\d'’\"():&]+)(?: <(?P<email>.+?)>)?$") | ||
|
||
|
||
|
@@ -231,34 +234,14 @@ def _get_author(self) -> dict[str, str | None]: | |
if not self._authors: | ||
return {"name": None, "email": None} | ||
|
||
m = AUTHOR_REGEX.match(self._authors[0]) | ||
|
||
if m is None: | ||
raise ValueError( | ||
"Invalid author string. Must be in the format: " | ||
"John Smith <[email protected]>" | ||
) | ||
|
||
name = m.group("name") | ||
email = m.group("email") | ||
|
||
name, email = parse_author(self._authors[0]) | ||
return {"name": name, "email": email} | ||
|
||
def _get_maintainer(self) -> dict[str, str | None]: | ||
if not self._maintainers: | ||
return {"name": None, "email": None} | ||
|
||
m = AUTHOR_REGEX.match(self._maintainers[0]) | ||
|
||
if m is None: | ||
raise ValueError( | ||
"Invalid maintainer string. Must be in the format: " | ||
"John Smith <[email protected]>" | ||
) | ||
|
||
name = m.group("name") | ||
email = m.group("email") | ||
|
||
name, email = parse_author(self._maintainers[0]) | ||
return {"name": name, "email": email} | ||
|
||
@property | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import warnings | ||
|
||
from contextlib import contextmanager | ||
from email.utils import parseaddr | ||
from pathlib import Path | ||
from typing import Any | ||
from typing import Iterator | ||
|
@@ -105,3 +106,23 @@ def readme_content_type(path: str | Path) -> str: | |
return "text/markdown" | ||
else: | ||
return "text/plain" | ||
|
||
|
||
def parse_author(address: str) -> tuple[str, str | None]: | ||
"""Parse name and address parts from an email address string. | ||
|
||
>>> parse_author("John Doe <[email protected]>") | ||
('John Doe', '[email protected]') | ||
|
||
:param address: the email address string to parse. | ||
:return: a 2-tuple with the parsed name and optional email address. | ||
:raises ValueError: if the parsed string does not contain a name. | ||
""" | ||
if "@" not in address: | ||
return address, None | ||
name, email = parseaddr(address) | ||
if not name or ( | ||
email and address not in [f"{name} <{email}>", f'"{name}" <{email}>'] | ||
): | ||
raise ValueError(f"Invalid author string: {address!r}") | ||
return name, email or None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,14 +55,11 @@ def test_package_authors() -> None: | |
def test_package_authors_invalid() -> None: | ||
package = Package("foo", "0.1.0") | ||
|
||
package.authors.insert(0, "<John Doe") | ||
package.authors.insert(0, "[email protected]") | ||
with pytest.raises(ValueError) as e: | ||
package.author_name | ||
|
||
assert ( | ||
str(e.value) | ||
== "Invalid author string. Must be in the format: John Smith <[email protected]>" | ||
) | ||
assert str(e.value) == "Invalid author string: '[email protected]'" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
|
@@ -78,11 +75,14 @@ def test_package_authors_invalid() -> None: | |
("Doe, John", None), | ||
("(Doe, John)", None), | ||
("John Doe", "[email protected]"), | ||
("Doe, John", "[email protected]"), | ||
("MyCompanyName R&D", "[email protected]"), | ||
("John-Paul: Doe", None), | ||
("John-Paul: Doe", "[email protected]"), | ||
("John Doe the 3rd", "[email protected]"), | ||
("<John Doe", None), | ||
("John? Doe", None), | ||
("Jane+Doe", None), | ||
("~John Doe", None), | ||
("John~Doe", None), | ||
], | ||
) | ||
def test_package_authors_valid(name: str, email: str | None) -> None: | ||
|
@@ -102,11 +102,8 @@ def test_package_authors_valid(name: str, email: str | None) -> None: | |
[ | ||
("<[email protected]>",), | ||
("[email protected]",), | ||
("<John Doe",), | ||
("John? Doe",), | ||
("Jane+Doe",), | ||
("~John Doe",), | ||
("John~Doe",), | ||
("Doe, John <[email protected]>",), | ||
("John-Paul: Doe <[email protected]>",), | ||
], | ||
) | ||
def test_package_author_names_invalid(name: str) -> None: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import pytest | ||
|
||
from poetry.core.utils.helpers import combine_unicode | ||
from poetry.core.utils.helpers import parse_author | ||
from poetry.core.utils.helpers import parse_requires | ||
from poetry.core.utils.helpers import readme_content_type | ||
from poetry.core.utils.helpers import temporary_directory | ||
|
@@ -118,3 +119,52 @@ def test_utils_helpers_readme_content_type( | |
readme: str | Path, content_type: str | ||
) -> None: | ||
assert readme_content_type(readme) == content_type | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"author, name, email", | ||
[ | ||
# Verify the (probable) default use case | ||
("John Doe <[email protected]>", "John Doe", "[email protected]"), | ||
# Name only | ||
("John Doe", "John Doe", None), | ||
# Name with a “special” character + email address | ||
( | ||
"R&D <[email protected]>", | ||
"R&D", | ||
"[email protected]", | ||
), | ||
# Name with a “special” character only | ||
("R&D", "R&D", None), | ||
# Name with fancy unicode character + email address | ||
( | ||
"my·fancy corp <[email protected]>", | ||
"my·fancy corp", | ||
"[email protected]", | ||
), | ||
# Name with fancy unicode character only | ||
("my·fancy corp", "my·fancy corp", None), | ||
], | ||
) | ||
def test_utils_helpers_parse_author(author: str, name: str, email: str | None) -> None: | ||
"""Test valid inputs for the :func:`parse_author` function.""" | ||
assert parse_author(author) == (name, email) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"author", | ||
[ | ||
# Email address only, wrapped in angular brackets | ||
"<[email protected]>", | ||
# Email address only | ||
"[email protected]", | ||
# Non-RFC-conform cases with unquoted commas | ||
"asf,[email protected]", | ||
"asf,<[email protected]>", | ||
"asf, [email protected]", | ||
], | ||
) | ||
def test_utils_helpers_parse_author_invalid(author: str) -> None: | ||
"""Test invalid inputs for the :func:`parse_author` function.""" | ||
with pytest.raises(ValueError): | ||
parse_author(author) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you stack this upon #517, please?