Skip to content
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

fix: qualifiers type annotation #172

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

jhoward-lm
Copy link

@jhoward-lm jhoward-lm commented Oct 22, 2024

This PR modifies the type annotation of the qualifiers attribute declaration from:

qualifiers: Union[str, Dict[str, str], None]

to:

qualifiers: Dict[str, str]

in order to reflect its true runtime type.

Closes #169.

Miscellaneous

  • fixed return type of the encode: "Optional[Literal[False]]" overload of normalize_qualifiers to Dict[str, str]
  • fixed return type of the encode: "Optional[Literal[False]]" overload of normalize to Dict[str, str] (the other tuple members in the return type unchanged)
  • updated instances of Union[AnyStr, Dict[str, str], None] to Optional[Union[AnyStr, Dict[str, str]]]
  • updated instances of Union[str, Dict[str, str], None] to Optional[Union[str, Dict[str, str]]]
  • removed unused # NOQA directives
  • updated return type of PackageURL.__new__ and PackageURL.from_string to typing_extensions.Self
  • updated first param name of PackageURL.__new__ from self to cls since it is a classmethod
  • removed quotes from type annotation forward references by adding from __future__ import annotations

Signed-off-by: Jonathan Howard <[email protected]>
@jhoward-lm
Copy link
Author

@tdruez @gruebel I don't have permissions to add reviewers, please take a look when you get a chance. Thanks!

Copy link
Contributor

@gruebel gruebel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good and locally it passes mypy 😄

@tdruez
Copy link
Collaborator

tdruez commented Oct 23, 2024

@jhoward-lm I see there are still Union[str, Dict[str, str], None] references in the source, should those be updated to Dict[str, str] as well?

@jhoward-lm
Copy link
Author

@tdruez I don't believe so. Those are generalized argument/parameter types, but the type gets narrowed before returning.

You did however draw my attention to several other typing issues; I'll push a commit shortly for review

@jhoward-lm
Copy link
Author

@tdruez @gruebel With my most recent comment, tests still pass locally, although the oldest version of python I was able to test with was 3.9 (3.7 and 3.8 are EOL so I couldn't install them with homebrew).

I'll update the PR description with more detail. Let me know your thoughts!

@@ -546,4 +541,4 @@ def from_string(cls, purl: str) -> "PackageURL":
encode=False,
)

return PackageURL(type, namespace, name, version, qualifiers, subpath)
return cls(type, namespace, name, version, qualifiers, subpath) # type: ignore[arg-type]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can easily fix this, then I don't need to create a separate PR 😅

Just create your own AnyStr type alias and remove the one coming from the typing module.

Add

AnyStr = Union[str, bytes]

directly under basestring at the top and remove the from typing import AnyStr. AnyStr was deprecated in Python 3.13 anyway.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! One small alteration: since it's only used for type hints, I put it into the TYPE_CHECKING block. Tests still pass locally. Are you good with that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, also sounds good

Signed-off-by: Jonathan Howard <[email protected]>
@jhoward-lm
Copy link
Author

@gruebel @tdruez
With the addition of from __future__ import annotations, the pipe style union operator as well as built-in collection types (Dict -> dict, Tuple -> tuple, etc.) are available. I verified by running pyupgrade --py37-plus src/packageurl/__init__.py.

So things like this could be updated across the board:

# before
Union[str, Dict[str, str], None]

# after
str | dict[str, str] | None

Let me know if you want me to go ahead with it, or I can hold off.

@gruebel
Copy link
Contributor

gruebel commented Oct 24, 2024

I actually like the newer syntax style more than the old one, but I would recommend to do it in a follow-up PR, because it is kind of like formatting and I don't like to mix actual changes with formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Qualifiers typing
3 participants