-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Untyped decorator makes function "foo" untyped #11763
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
Comments
What version of |
I'm not aware of any changes in mypy 0.920 that would affect this. As Gary says, make sure you're using pytest>=6 and that it is installed in the same virtual environment as mypy (or that you use the |
Just as a point of reference: We also started getting decorator-related mypy errors with mypy 0.920: _R = TypeVar("_R")
_P = ParamSpec("_P")
def transaction(f: Callable[Concatenate[Transaction, _P], _R]) -> Callable[_P, _R]: ...
@transaction
def foo(): ...
x = foo() # Need type annotation for "x" [var-annotated] This didn't trigger an error before, although def transaction(f: Callable[..., _R]) -> Callable[..., _R]: ... |
I'm using pytest 6.2.5 (the latest version). |
Based on @srittau's comment, is it safe to assume that this is a bug in mypy and not a lack of typing support in pytest? It seems like this error is raised even when the decorator is typed and no external libraries are used. |
What happens when you run mypy without
That problem seems unrelated to your issue. Those errors are probably due to incomplete |
I get an error saying it can't find type hints: $ mypy --strict test.py
test.py:1: error: Cannot find implementation or library stub for module named "pytest"
test.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
test.py:3: error: Untyped decorator makes function "foo" untyped
Found 2 errors in 1 file (checked 1 source file) However, pytest is installed and in both my $ which pytest
/Users/Adam/.spack/.spack-env/view/bin/pytest
$ python
Python 3.9.9 (main, Dec 15 2021, 16:29:14)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pytest
>>> pytest.__file__
'/Users/Adam/.spack/.spack-env/view/lib/python3.8/site-packages/pytest/__init__.py' If I add |
What is the result of:
|
$ python -m pip show pytest
Name: pytest
Version: 6.2.4
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email: None
License: MIT
Location: /Users/Adam/.spack/.spack-env/._view/tmizjnr2yebouizawyxzmo3gwignw5lz/lib/python3.8/site-packages
Requires: attrs, iniconfig, packaging, pluggy, py, toml
Required-by: pytest-mock, pytest-cov, nbmake
$ python -m pip show mypy
Name: mypy
Version: 0.920
Summary: Optional static typing for Python
Home-page: http://www.mypy-lang.org/
Author: Jukka Lehtosalo
Author-email: [email protected]
License: MIT License
Location: /Users/Adam/spack/opt/spack/darwin-catalina-ivybridge/apple-clang-12.0.0/py-mypy-0.920-fimyp26g6xfazrl4ibu5mvkwlwd33egu/lib/python3.9/site-packages
Requires: typing-extensions, mypy-extensions, tomli
Required-by:
$ python -m mypy -c 'import pytest; reveal_type(pytest.fixture)'
<string>:1: note: Revealed type is "Any" Note that this is on my local installation which uses Spack, not pip. |
I don't know anything about Spack, is it doing something weird to If |
Ah yes, Spack does many weird things. All packages are installed to a separate installation prefix allowing you to install multiple versions of the same package with different compilers. I just tried again with only Python 3.9 versions of mypy and pytest in my PYTHONPATH and I'm still getting the same error. Let me try on another system using conda/pip instead of Spack. |
This seems to work inside a Conda environment: $ python -m mypy -c 'import pytest; reveal_type(pytest.fixture)'
<string>:1: note: Revealed type is "Overload(def [_FixtureFunction <: def (*Any, **Any) -> builtins.object] (fixture_function: _FixtureFunction`-1, *, scope: Union[Union[Literal['session'], Literal['package'], Literal['module'], Literal['class'], Literal['function']], def (builtins.str, _pytest.config.Config) -> Union[Literal['session'], Literal['package'], Literal['module'], Literal['class'], Literal['function']]] =, params: Union[typing.Iterable[builtins.object], None] =, autouse: builtins.bool =, ids: Union[typing.Iterable[Union[None, builtins.str, builtins.float, builtins.int, builtins.bool]], def (Any) -> Union[builtins.object, None], None] =, name: Union[builtins.str, None] =) -> _FixtureFunction`-1, def (fixture_function: None =, *, scope: Union[Union[Literal['session'], Literal['package'], Literal['module'], Literal['class'], Literal['function']], def (builtins.str, _pytest.config.Config) -> Union[Literal['session'], Literal['package'], Literal['module'], Literal['class'], Literal['function']]] =, params: Union[typing.Iterable[builtins.object], None] =, autouse: builtins.bool =, ids: Union[typing.Iterable[Union[None, builtins.str, builtins.float, builtins.int, builtins.bool]], def (Any) -> Union[builtins.object, None], None] =, name: Union[builtins.str, None] =) -> _pytest.fixtures.FixtureFunctionMarker)" Which raises the question of why it doesn't work with Spack or with GitHub Actions. The latter doesn't use Spack at all. Where does mypy search for type hints? |
I believe the answer to this question is https://mypy.readthedocs.io/en/stable/running_mypy.html#finding-imports I'm guessing something gets found before the actual pytest |
I don't think this is what's happening. The earlier error message you posted said mypy couldn't find implementation or library stub for pytest, and if I remember correctly we emit a different error when we find a module but no type hints.
I'm guessing this is why Spack doesn't work. mypy just searches the normal site-packages directories and ignores For Github Actions, do you have a CI run that I could look at with those errors? I've been looking at some recent CI runs on microsoft/torchgeo (which I'm guessing is the project this bug is taken from) but I can't find any untyped decorator errors. Also, since this hasn't been mentioned yet: the errors you're getting are because of the |
Ah, that's unfortunate. I'll just set $ mypy .
mypy: "../.spack/.spack-env/view/lib/python3.8/site-packages/typing_extensions.py" shadows library module "typing_extensions"
note: A user-defined top-level module with name "typing_extensions" is not supported Is that expected?
Yep, that's the one. Here is one example, which occurs even after I force installation of mypy 0.910: https://github.com/microsoft/torchgeo/runs/4554198611?check_suite_focus=true. Now I'm trying to use 0.920 again and I'm no longer seeing the issue?? https://github.com/microsoft/torchgeo/runs/4556423997?check_suite_focus=true. I'm starting to lose my mind on this one lol. |
I'm not sure. Judging from https://mypy.readthedocs.io/en/stable/stubs.html?highlight=site-packages#creating-a-stub, I don't think pointing MYPYPATH at the site-packages directory is supported:
If I'm reading the docs correctly, Spack seems to be trying to emulate a regular virtualenv, so some ideas for getting this to work (after undoing the MYPYPATH change):
If that doesn't work, could you try seeing what the output of Out of curiosity, is mypy not working with Spack a new occurrence, or has this always happened? For the Github Actions errors, that seems to be unrelated to the mypy version. During the first CI run, the setup runs |
Closing, since I don't think there's anything actionable on mypy's side |
Bug Report
Our CI tests started failing with the mypy 0.920 release. It looks like a new feature is checking decorator types? This may not be a bug in mypy but a lack of documentation explaining the new feature. I couldn't find any release notes or CHANGELOG explaining this feature.
To Reproduce
mypy --strict --ignore-missing-imports foo.py
Expected Behavior
Since the function is correctly typed, I wouldn't expect mypy to complain, but there must be a new feature triggering this. Is it because the pytest library is untyped? Normally mypy skips those kind of things.
Actual Behavior
Mypy complains with an error message I can't find in the documentation:
Your Environment
--strict --ignore-missing-imports
mypy.ini
(and other config files): N/A@calebrob6
The text was updated successfully, but these errors were encountered: