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

Enable strict mode for mypy + other error codes #149

Merged
merged 3 commits into from
Oct 2, 2022

Conversation

mkniewallner
Copy link
Collaborator

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • Documentation in docs is updated

Description of changes

Enable strict mode for mypy, which enables the following flags, according to --strict documentation:

$ poetry run mypy --help | grep -A 8 "\--strict "
  --strict                  Strict mode; enables the following flags: --warn-
                            unused-configs, --disallow-any-generics,
                            --disallow-subclassing-any, --disallow-untyped-
                            calls, --disallow-untyped-defs, --disallow-
                            incomplete-defs, --check-untyped-defs, --disallow-
                            untyped-decorators, --no-implicit-optional,
                            --warn-redundant-casts, --warn-unused-ignores,
                            --warn-return-any, --no-implicit-reexport,
                            --strict-equality, --strict-concatenate

Also enable:

  • warn_unreachable to warn about unreachable code
  • ignore-without-code to avoid using ignore without specifying specific error codes
  • redundant-expr to avoid redundant/useless expressions
  • truthy-bool to avoid conditional checks where the result would always be True
  • pretty to better see the location of an error reported by mypy. For instance:
deptry/json_writer.py:16: error: Missing type parameters for generic type "Dict"  [type-arg]
        def write(self, issues: Dict) -> None:
                                ^

Raw list of errors reported by mypy after activating the checks:

$ poetry run mypy
deptry/notebook_import_extractor.py:29: error: Missing type parameters for generic type "dict"  [type-arg]
        def _read_ipynb_file(path_to_ipynb: Path) -> dict:
                                                     ^
deptry/notebook_import_extractor.py:32: error: Returning Any from function declared to return "Dict[Any, Any]"  [no-any-return]
            return notebook
            ^~~~~~~~~~~~~~~
deptry/notebook_import_extractor.py:35: error: Missing type parameters for generic type "dict"  [type-arg]
        def _keep_code_cells(notebook: dict) -> List[dict]:
                                       ^
deptry/notebook_import_extractor.py:42: error: Missing type parameters for generic type "dict"  [type-arg]
        def _extract_import_statements_from_cell(self, cell: dict) -> List[str]:
                                                             ^
deptry/notebook_import_extractor.py:46: error: Missing type parameters for generic type "List"  [type-arg]
        def _flatten(list_of_lists: List[List]) -> List:
                                         ^
deptry/json_writer.py:16: error: Missing type parameters for generic type "Dict"  [type-arg]
        def write(self, issues: Dict) -> None:
                                ^
deptry/dependency.py:5: error: Module "deptry.compat" does not explicitly export attribute "PackageNotFoundError"; implicit reexport disabled  [attr-defined]
    from deptry.compat import PackageNotFoundError, metadata
    ^
deptry/module.py:7: error: Module "deptry.compat" does not explicitly export attribute "PackageNotFoundError"; implicit reexport disabled  [attr-defined]
    from deptry.compat import PackageNotFoundError, metadata
    ^
deptry/module.py:132: error: Skipping analyzing "deptry.stdlibs.py310": module is installed, but missing library stubs or py.typed marker  [import]
                    from deptry.stdlibs.py310 import stdlib
    ^
deptry/module.py:135: error: Returning Any from function declared to return "Set[str]"  [no-any-return]
                return stdlib
                ^~~~~~~~~~~~~
deptry/dependency_getter/requirements_txt.py:120: error: Missing type parameters for generic type "Match"  [type-arg]
        def _line_is_url(line: str) -> Optional[re.Match]:
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deptry/import_parser.py:52: error: "type: ignore" comment without error code (consider "type: ignore[call-overload]" instead)  [ignore-without-code]
                    root = ast.parse(f.read(), path_to_py_file)  # type: ignore
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deptry/import_parser.py:61: error: "type: ignore" comment without error code (consider "type: ignore[call-overload]" instead)  [ignore-without-code]
                    root = ast.parse(f.read(), path_to_py_file)  # type: ignore
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deptry/import_parser.py:98: error: Left operand of "and" is always true  [redundant-expr]
                elif isinstance(node, ast.ImportFrom) and node.module and node.level == 0:
                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deptry/import_parser.py:103: error: Missing type parameters for generic type "List"  [type-arg]
        def _flatten_list(modules_per_file: List[List]) -> List:
                                                 ^
deptry/utils.py:36: error: Missing type parameters for generic type "Dict"  [type-arg]
    def load_pyproject_toml(pyproject_toml_path: str = PYPROJECT_TOML_PATH) -> Dict:
                                                                               ^
deptry/dependency_getter/pyproject_toml.py:40: error: Returning Any from function declared to return "Dict[str, Any]"  [no-any-return]
            return dependencies
            ^~~~~~~~~~~~~~~~~~~
deptry/dependency_getter/pyproject_toml.py:69: error: Missing type parameters for generic type "dict"  [type-arg]
        def _is_optional(dep: str, spec: Union[str, dict]) -> bool:
                                                    ^
deptry/dependency_getter/pyproject_toml.py:76: error: Missing type parameters for generic type "dict"  [type-arg]
        def _is_conditional(dep: str, spec: Union[str, dict]) -> bool:
                                                       ^
deptry/core.py:8: error: Skipping analyzing "deptry.dependency_getter.pyproject_toml": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.dependency_getter.pyproject_toml import PyprojectTomlDependencyGetter
    ^
deptry/core.py:8: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
deptry/core.py:9: error: Skipping analyzing "deptry.dependency_getter.requirements_txt": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.dependency_getter.requirements_txt import RequirementsTxtDependencyGetter
    ^
deptry/core.py:12: error: Skipping analyzing "deptry.issue_finders.misplaced_dev": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.issue_finders.misplaced_dev import MisplacedDevDependenciesFinder
    ^
deptry/core.py:13: error: Skipping analyzing "deptry.issue_finders.missing": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.issue_finders.missing import MissingDependenciesFinder
    ^
deptry/core.py:14: error: Skipping analyzing "deptry.issue_finders.obsolete": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.issue_finders.obsolete import ObsoleteDependenciesFinder
    ^
deptry/core.py:15: error: Skipping analyzing "deptry.issue_finders.transitive": module is installed, but missing library stubs or py.typed marker  [import]
    from deptry.issue_finders.transitive import TransitiveDependenciesFinder
    ^
deptry/cli.py:211: error: "root" has type "Path" which does not implement __bool__ or __len__ so it could always be true in boolean context  [truthy-bool]
        if not root:
               ^~~~
Found 26 errors in 10 files (checked 26 source files)

@mkniewallner mkniewallner assigned fpgmaas and unassigned fpgmaas Oct 1, 2022
@mkniewallner mkniewallner force-pushed the strict-mode-mypy branch 2 times, most recently from 1236867 to d1b4f96 Compare October 1, 2022 19:18
@codecov-commenter
Copy link

codecov-commenter commented Oct 1, 2022

Codecov Report

Merging #149 (69afb8e) into main (55aa5fd) will decrease coverage by 0.1%.
The diff coverage is 96.8%.

@@           Coverage Diff           @@
##            main    #149     +/-   ##
=======================================
- Coverage   92.3%   92.2%   -0.2%     
=======================================
  Files         39      42      +3     
  Lines       1342    1348      +6     
=======================================
+ Hits        1240    1243      +3     
- Misses       102     105      +3     
Impacted Files Coverage Δ
deptry/cli.py 73.0% <66.6%> (ø)
deptry/compat.py 71.4% <100.0%> (+4.7%) ⬆️
deptry/dependency.py 92.9% <100.0%> (ø)
deptry/dependency_getter/pyproject_toml.py 100.0% <100.0%> (ø)
deptry/dependency_getter/requirements_txt.py 96.5% <100.0%> (ø)
deptry/import_parser.py 96.7% <100.0%> (ø)
deptry/issue_finders/transitive.py 96.0% <100.0%> (+0.1%) ⬆️
deptry/json_writer.py 100.0% <100.0%> (ø)
deptry/module.py 89.7% <100.0%> (+0.1%) ⬆️
deptry/notebook_import_extractor.py 100.0% <100.0%> (ø)
... and 4 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@mkniewallner
Copy link
Collaborator Author

69afb8e is a good occasion to see #138 (comment) happening in the wild.

@fpgmaas fpgmaas merged commit d6dd9c4 into fpgmaas:main Oct 2, 2022
fpgmaas pushed a commit that referenced this pull request Oct 2, 2022
* chore(mypy): enable strict mode and more error codes
* chore: comply with `mypy` new rules
* chore: ignore some type errors related to `importlib_metadata`
@mkniewallner mkniewallner deleted the strict-mode-mypy branch November 6, 2022 21:44
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.

3 participants