Skip to content

Pre-commit: add mypy #4321

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

Closed
wants to merge 2 commits into from
Closed

Conversation

cclauss
Copy link
Member

@cclauss cclauss commented Apr 6, 2021

Describe your change:

  • Add mypy tests to pre-commit
  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@cclauss cclauss requested a review from dhruvmanila as a code owner April 6, 2021 14:23
@ghost ghost added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass labels Apr 6, 2021
@dhruvmanila
Copy link
Member

As mentioned in this comment #4315 (comment), there's going to be problem when running mypy in pre-commit.

The problem with running mypy in pre-commit is that pre-commit runs every hook in its own virtual environment and as mypy needs the information from project dependencies, they need to be installed there as well. pre-commit does provide an option: - additional_dependencies: ['numpy', ...] but that would mean we need to maintain the dependencies in requirements.txt and in pre-commit-config.yaml.

- repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.812
    hooks:
      - id: mypy
        additional_dependencies: [numpy, beautifulsoup4, requests, ...]

Ref:

Our `mypy.ini` file does `--ignore-missing-imports` so our tests should pass even if we have none of our dependencies.  I sense that mypy in our pre-commit is not finding / reading our `mypy.ini` file.
@dhruvmanila
Copy link
Member

dhruvmanila commented Apr 7, 2021

mypy or any other type checkers should be run in an environment which contains the project dependencies for it to correctly report errors. If we are using any function/class/variable from other modules then mypy will know whether we are passing the correct type of arguments or assigning the correct type to the variable, etc.

This will be easier to understand when we start running mypy in a strict fashion as I am for algorithms-keeper. I faced the same issue when using mypy in pre-commit for the bot. At the end, it was either of the options:

  1. Run mypy in pre-commit with specifying the additional_dependencies
  2. Run mypy as a local hook in pre-commit
  3. Run mypy in CI

@cclauss
Copy link
Member Author

cclauss commented Apr 7, 2021

Makes sense.

@cclauss cclauss closed this Apr 7, 2021
@cclauss cclauss deleted the pre-commit_add_mypy branch April 7, 2021 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files tests are failing Do not merge until tests pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants