Skip to content

Commit

Permalink
Fix/ignore mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Apr 26, 2024
1 parent 2c45906 commit 96e1d9f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
23 changes: 11 additions & 12 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ exclude = (?x)(
| ^.tox/
| ^.egg/
| ^pkg_resources/tests/data/my-test-package-source/setup.py$ # Duplicate module name
| ^.+?/(_vendor|extern)/ # Vendored
| ^pkg_resources/_vendor/ # Vendored
| ^setuptools/_vendor/ # Vendored
| ^setuptools/_distutils/ # Vendored
| ^setuptools/config/_validate_pyproject/ # Auto-generated
)
Expand All @@ -21,16 +22,14 @@ exclude = (?x)(
# *.extern modules that actually live in *._vendor will also cause attr-defined issues on import
disable_error_code = attr-defined

# - Avoid raising issues when importing from "extern" modules, as those are added to path dynamically.
# https://github.com/pypa/setuptools/pull/3979#discussion_r1367968993
[mypy-pkg_resources._vendor.*,setuptools._vendor.*,setuptools.config._validate_pyproject.*,setuptools.tests.*]
# Even when excluded there might be problems: python/mypy/issues/11936#issuecomment-1466764006
# - Avoid raising issues when importing from "_vendor" modules
# - Avoid raising issues when importing dependencies in tests
follow_imports = silent
# silent => ignore errors when following imports

[mypy-distutils._modified,jaraco.*]
# - distutils._modified has different errors on Python 3.8 [import-untyped], on Python 3.9+ [import-not-found]
# - All jaraco modules are still untyped
# - _validate_project sometimes complains about trove_classifiers (#4296)
[mypy-pkg_resources.extern.*,setuptools.extern.*,distutils._modified,jaraco.*,trove_classifiers]
# - Some jaraco modules are still untyped
ignore_missing_imports = True

# - pkg_resources tests create modules that won't exists statically before the test is run.
# Let's ignore all "import-not-found" since, if an import really wasn't found, then the test would fail.
# - setuptools._vendor.packaging._manylinux: Mypy issue, this vendored module is already excluded!
[mypy-pkg_resources.tests.*,setuptools._vendor.packaging._manylinux,setuptools.config._validate_pyproject.*]
disable_error_code = import-not-found
13 changes: 9 additions & 4 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@
join_continuation,
)

from pkg_resources._vendor import platformdirs
from pkg_resources._vendor import packaging
from ._vendor import platformdirs
from ._vendor import packaging
from ._vendor.packaging.requirements import (
# These imports are explicit only to avoid mypy error
InvalidRequirement as _packaging_InvalidRequirement,
Requirement as _packaging_Requirement,
)

__import__('pkg_resources._vendor.packaging.version')
__import__('pkg_resources._vendor.packaging.specifiers')
Expand Down Expand Up @@ -3123,11 +3128,11 @@ def parse_requirements(strs):
return map(Requirement, join_continuation(map(drop_comment, yield_lines(strs))))


class RequirementParseError(packaging.requirements.InvalidRequirement):
class RequirementParseError(_packaging_InvalidRequirement):
"Compatibility wrapper for InvalidRequirement"


class Requirement(packaging.requirements.Requirement):
class Requirement(_packaging_Requirement):
def __init__(self, requirement_string):
"""DO NOT CALL THIS UNDOCUMENTED METHOD; use Requirement.parse()!"""
super().__init__(requirement_string)
Expand Down
4 changes: 2 additions & 2 deletions pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def teardown_class(cls):
finalizer()

def test_resource_listdir(self):
import mod
import mod # type: ignore[import-not-found]

zp = pkg_resources.ZipProvider(mod)

Expand All @@ -88,7 +88,7 @@ def test_resource_listdir(self):
assert zp.resource_listdir('nonexistent') == []
assert zp.resource_listdir('nonexistent/') == []

import mod2
import mod2 # type: ignore[import-not-found]

zp2 = pkg_resources.ZipProvider(mod2)

Expand Down
6 changes: 3 additions & 3 deletions pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,11 @@ def test_two_levels_deep(self, symlinked_tmpdir):
(pkg1 / '__init__.py').write_text(self.ns_str, encoding='utf-8')
(pkg2 / '__init__.py').write_text(self.ns_str, encoding='utf-8')
with pytest.warns(DeprecationWarning, match="pkg_resources.declare_namespace"):
import pkg1
import pkg1 # type: ignore[import-not-found]
assert "pkg1" in pkg_resources._namespace_packages
# attempt to import pkg2 from site-pkgs2
with pytest.warns(DeprecationWarning, match="pkg_resources.declare_namespace"):
import pkg1.pkg2
import pkg1.pkg2 # type: ignore[import-not-found]
# check the _namespace_packages dict
assert "pkg1.pkg2" in pkg_resources._namespace_packages
assert pkg_resources._namespace_packages["pkg1"] == ["pkg1.pkg2"]
Expand Down Expand Up @@ -862,7 +862,7 @@ def test_path_order(self, symlinked_tmpdir):
(subpkg / '__init__.py').write_text(vers_str % number, encoding='utf-8')

with pytest.warns(DeprecationWarning, match="pkg_resources.declare_namespace"):
import nspkg.subpkg
import nspkg.subpkg # type: ignore[import-not-found]
import nspkg
expected = [str(site.realpath() / 'nspkg') for site in site_dirs]
assert nspkg.__path__ == expected
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/_apply_pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def _project_urls(dist: "Distribution", val: dict, _root_dir):
_set_config(dist, "project_urls", val)


def _python_requires(dist: "Distribution", val: dict, _root_dir):
def _python_requires(dist: "Distribution", val: str, _root_dir):
from .._vendor.packaging.specifiers import SpecifierSet

_set_config(dist, "python_requires", SpecifierSet(val))
Expand Down

0 comments on commit 96e1d9f

Please sign in to comment.