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

Typing annotations fixed in version.py #723

Merged
merged 1 commit into from
Sep 12, 2023

Conversation

jolaf
Copy link
Contributor

@jolaf jolaf commented Sep 10, 2023

I've run my app under typeguard and it found couple of typing annotation issues that I fix here.

@jolaf
Copy link
Contributor Author

jolaf commented Sep 10, 2023

See also pypa/setuptools#4044

@brettcannon
Copy link
Member

Since mypy didn't report any type issues with this code, I wonder if the None checks are actually wrong instead of the type annotations?

@jolaf
Copy link
Contributor Author

jolaf commented Sep 11, 2023

mypy has a very limited capability in reading the real logic of the code, it mostly checks declarations and assignments.
If you check a variable for None, it would not expect you to declare None as a valid value.

typeguard is a runtime checker, it checks what really gets passed into a method.

Under typeguard, simply importing pkg_resources fails like this:

test.py:

from typeguard import install_import_hook
with install_import_hook(('pkg_resources',)):
    import pkg_resources
$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import pkg_resources
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_importhook.py", line 98, in exec_module
    super().exec_module(module)
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3328, in <module>
    def _initialize_master_working_set():
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3302, in _call_aside
    f(*args, **kwargs)
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 3340, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 622, in _build_master
    ws = cls()
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 615, in __init__
    self.add_entry(entry)
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 671, in add_entry
    for dist in find_distributions(entry, True):
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2134, in find_on_path
    for dist in factory(fullpath):
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2199, in distributions_from_metadata
    yield Distribution.from_location(
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2665, in from_location
    return cls(
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 2646, in __init__
    self._version = safe_version(version)
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/__init__.py", line 1398, in safe_version
    return str(packaging.version.Version(version))
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/_vendor/packaging/version.py", line 204, in __init__
    pre=_parse_letter_version(match.group("pre_l"), match.group("pre_n")),
  File "/home/jolaf/.local/lib/python3.10/site-packages/pkg_resources/_vendor/packaging/version.py", line 453, in _parse_letter_version
    def _parse_letter_version(
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_functions.py", line 138, in check_argument_types
    check_type_internal(value, annotation, memo)
  File "/home/jolaf/.local/lib/python3.10/site-packages/typeguard/_checkers.py", line 764, in check_type_internal
    raise TypeCheckError(f"is not an instance of {qualified_name(origin_type)}")
typeguard.TypeCheckError: argument "letter" (None) is not an instance of str

So, a None gets actually passed as a letter argument.
You can investigate the stack trace (ignoring typeguard lines) and find out how it happens (both match.group() calls in version.py:204 return None).
Maybe it's a bug in the code and letter should never be None.
But without typeguard the code works fine, so I suppose that None is a valid value, and typing annotation should reflect that.

@brettcannon
Copy link
Member

But without typeguard the code works fine, so I suppose that None is a valid value, and typing annotation should reflect that.

I don't know if that's necessarily true that typing "should reflect that" as an empty e.g. dict would also work since there's no specific check for None but for false-y values (hence my question of what triggered this).

But since other parameters do look to be expecting None as a possible value, I'm fine with it.

P.S.: I'm assuming typeguard is https://pypi.org/project/typeguard/ .

@brettcannon brettcannon merged commit b509bef into pypa:main Sep 12, 2023
29 checks passed
@jolaf
Copy link
Contributor Author

jolaf commented Sep 12, 2023

Yep, that's right. Thanks!

kodiakhq bot referenced this pull request in cloudquery/plugin-sdk-python Nov 1, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [packaging](https://github.com/pypa/packaging) | minor | `==23.1` -> `==23.2` |

---

### Release Notes

<details>
<summary>pypa/packaging (packaging)</summary>

### [`v23.2`](https://github.com/pypa/packaging/releases/tag/23.2)

[Compare Source](https://github.com/pypa/packaging/compare/23.1...23.2)

#### What's Changed

-   parse_marker should consume the entire source string by [@&#8203;mwerschy](https://github.com/mwerschy) in [https://github.com/pypa/packaging/pull/687](https://github.com/pypa/packaging/pull/687)
-   Create a Security Policy file  by [@&#8203;joycebrum](https://github.com/joycebrum) in [https://github.com/pypa/packaging/pull/695](https://github.com/pypa/packaging/pull/695)
-   Add python 3.12 to CI by [@&#8203;mayeut](https://github.com/mayeut) in [https://github.com/pypa/packaging/pull/689](https://github.com/pypa/packaging/pull/689)
-   Remove URL validation from requirement parsing by [@&#8203;uranusjr](https://github.com/uranusjr) in [https://github.com/pypa/packaging/pull/684](https://github.com/pypa/packaging/pull/684)
-   Add types for packaging.version.\_Version by [@&#8203;hauntsaninja](https://github.com/hauntsaninja) in [https://github.com/pypa/packaging/pull/665](https://github.com/pypa/packaging/pull/665)
-   Add PyPy 3.10 to CI by [@&#8203;mayeut](https://github.com/mayeut) in [https://github.com/pypa/packaging/pull/699](https://github.com/pypa/packaging/pull/699)
-   Remove unused argument in `_manylinux._is_compatible` by [@&#8203;mayeut](https://github.com/mayeut) in [https://github.com/pypa/packaging/pull/700](https://github.com/pypa/packaging/pull/700)
-   Canonicalize names for requirements comparison by [@&#8203;astrojuanlu](https://github.com/astrojuanlu) in [https://github.com/pypa/packaging/pull/696](https://github.com/pypa/packaging/pull/696)
-   Add platform tag support for LoongArch by [@&#8203;loongson-zn](https://github.com/loongson-zn) in [https://github.com/pypa/packaging/pull/693](https://github.com/pypa/packaging/pull/693)
-   Ability to install `armv7l manylinux/musllinux` wheels on `armv8l` by [@&#8203;mayeut](https://github.com/mayeut) in [https://github.com/pypa/packaging/pull/690](https://github.com/pypa/packaging/pull/690)
-   Include CHANGELOG.rst in sdist by [@&#8203;astrojuanlu](https://github.com/astrojuanlu) in [https://github.com/pypa/packaging/pull/704](https://github.com/pypa/packaging/pull/704)
-   Update pyupgrade to Python 3.7+ by [@&#8203;fangchenli](https://github.com/fangchenli) in [https://github.com/pypa/packaging/pull/580](https://github.com/pypa/packaging/pull/580)
-   Fix version pattern pre-releases by [@&#8203;deathaxe](https://github.com/deathaxe) in [https://github.com/pypa/packaging/pull/705](https://github.com/pypa/packaging/pull/705)
-   Fix typos found by codespell by [@&#8203;DimitriPapadopoulos](https://github.com/DimitriPapadopoulos) in [https://github.com/pypa/packaging/pull/706](https://github.com/pypa/packaging/pull/706)
-   Support enriched metadata by [@&#8203;brettcannon](https://github.com/brettcannon) in [https://github.com/pypa/packaging/pull/686](https://github.com/pypa/packaging/pull/686)
-   Correct rST syntax in CHANGELOG.rst by [@&#8203;atugushev](https://github.com/atugushev) in [https://github.com/pypa/packaging/pull/709](https://github.com/pypa/packaging/pull/709)
-   fix: platform tag for GraalPy by [@&#8203;mayeut](https://github.com/mayeut) in [https://github.com/pypa/packaging/pull/711](https://github.com/pypa/packaging/pull/711)
-   Document that this library uses a calendar-based versioning scheme by [@&#8203;faph](https://github.com/faph) in [https://github.com/pypa/packaging/pull/717](https://github.com/pypa/packaging/pull/717)
-   fix: Update copyright date for docs by [@&#8203;garrypolley](https://github.com/garrypolley) in [https://github.com/pypa/packaging/pull/713](https://github.com/pypa/packaging/pull/713)
-   Bump pip version to avoid known vulnerabilities by [@&#8203;joycebrum](https://github.com/joycebrum) in [https://github.com/pypa/packaging/pull/720](https://github.com/pypa/packaging/pull/720)
-   Typing annotations fixed in version.py by [@&#8203;jolaf](https://github.com/jolaf) in [https://github.com/pypa/packaging/pull/723](https://github.com/pypa/packaging/pull/723)
-   parse\_{sdist,wheel}\_filename: don't raise InvalidVersion by [@&#8203;SpecLad](https://github.com/SpecLad) in [https://github.com/pypa/packaging/pull/721](https://github.com/pypa/packaging/pull/721)
-   Fix code blocks in CHANGELOG.md by [@&#8203;edmorley](https://github.com/edmorley) in [https://github.com/pypa/packaging/pull/724](https://github.com/pypa/packaging/pull/724)

#### New Contributors

-   [@&#8203;mwerschy](https://github.com/mwerschy) made their first contribution in [https://github.com/pypa/packaging/pull/687](https://github.com/pypa/packaging/pull/687)
-   [@&#8203;joycebrum](https://github.com/joycebrum) made their first contribution in [https://github.com/pypa/packaging/pull/695](https://github.com/pypa/packaging/pull/695)
-   [@&#8203;astrojuanlu](https://github.com/astrojuanlu) made their first contribution in [https://github.com/pypa/packaging/pull/696](https://github.com/pypa/packaging/pull/696)
-   [@&#8203;loongson-zn](https://github.com/loongson-zn) made their first contribution in [https://github.com/pypa/packaging/pull/693](https://github.com/pypa/packaging/pull/693)
-   [@&#8203;fangchenli](https://github.com/fangchenli) made their first contribution in [https://github.com/pypa/packaging/pull/580](https://github.com/pypa/packaging/pull/580)
-   [@&#8203;deathaxe](https://github.com/deathaxe) made their first contribution in [https://github.com/pypa/packaging/pull/705](https://github.com/pypa/packaging/pull/705)
-   [@&#8203;DimitriPapadopoulos](https://github.com/DimitriPapadopoulos) made their first contribution in [https://github.com/pypa/packaging/pull/706](https://github.com/pypa/packaging/pull/706)
-   [@&#8203;atugushev](https://github.com/atugushev) made their first contribution in [https://github.com/pypa/packaging/pull/709](https://github.com/pypa/packaging/pull/709)
-   [@&#8203;faph](https://github.com/faph) made their first contribution in [https://github.com/pypa/packaging/pull/717](https://github.com/pypa/packaging/pull/717)
-   [@&#8203;garrypolley](https://github.com/garrypolley) made their first contribution in [https://github.com/pypa/packaging/pull/713](https://github.com/pypa/packaging/pull/713)
-   [@&#8203;jolaf](https://github.com/jolaf) made their first contribution in [https://github.com/pypa/packaging/pull/723](https://github.com/pypa/packaging/pull/723)
-   [@&#8203;SpecLad](https://github.com/SpecLad) made their first contribution in [https://github.com/pypa/packaging/pull/721](https://github.com/pypa/packaging/pull/721)
-   [@&#8203;edmorley](https://github.com/edmorley) made their first contribution in [https://github.com/pypa/packaging/pull/724](https://github.com/pypa/packaging/pull/724)

**Full Changelog**: pypa/packaging@23.1...23.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on the first day of the month" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4xMDkuNCIsInVwZGF0ZWRJblZlciI6IjM2LjEwOS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants