Errors reported for 3rd-party dependencies
sobolevn opened this issue ยท 8 comments
When I run type-checking of my code with mypy
- everything works correctly.
But, when I try to run type-tests on some module, then it breaks, example:
E ../../../../../../Users/sobolev/Documents/github/returns/.venv/lib/python3.7/site-packages/hypothesis/vendor/pretty:706: error: Call to untyped function "_safe_getattr" in typed context (diff)
Same, when I type check my Django project with mypy
it works fine.
I tried adding this plugin, and running any test fails with:
/Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/tests/test_foo.yml:6:
E pytest_mypy_plugins.utils.TypecheckAssertionError: Invalid output:
E Expected:
E main:4: note: Revealed type is 'builtins.str' (diff)
E Actual:
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/base_useri:18: error: Need type annotation for 'password' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/base_useri:19: error: Need type annotation for 'last_login' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:23: error: Need type annotation for 'name' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:24: error: Need type annotation for 'content_type' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:25: error: Need type annotation for 'codename' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:34: error: Need type annotation for 'name' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:35: error: Need type annotation for 'permissions' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:57: error: Need type annotation for 'is_superuser' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:58: error: Need type annotation for 'groups' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:59: error: Need type annotation for 'user_permissions' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:70: error: Need type annotation for 'username' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:71: error: Need type annotation for 'first_name' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:72: error: Need type annotation for 'last_name' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:73: error: Need type annotation for 'email' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:74: error: Need type annotation for 'is_staff' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:75: error: Need type annotation for 'is_active' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/auth/modelsi:76: error: Need type annotation for 'date_joined' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/sites/modelsi:17: error: Need type annotation for 'domain' (diff)
E ../../../../../../Users/silviogutierrez/Sites/silviogutierrez/reactivated/master/.venv/lib/python3.8/site-packages/django-stubs/contrib/sites/modelsi:18: error: Need type annotation for 'name' (diff)
Basically, it seems you can't use this plugin with any 3rd-party typed dependency. Be it stubs or just a fully first-party typed library.
I did fix this by ignore the error in the mypy configuration:
[mypy]
plugins = strawberry.ext.mypy_plugin
check_untyped_defs = True
ignore_errors = False
ignore_missing_imports = True
strict_optional = True
[mypy-graphql.*]
ignore_errors = True
I tried this and it didn't work for me, mainly because these are stub files and not an actual module.
Neither django-stubs.*
or django.*
worked. I'm guessing for truly typed libraries, it'll work. But even then, you lose typing that you count on for your normal Mypy checks.
I fixed it by adding this beauty to tests/conftest.pf
:
import site
from typing import Any
def pytest_configure(config: Any) -> None:
# See: https://github.com/typeddjango/pytest-mypy-plugins/issues/34
site_packages = site.getsitepackages()[0]
for file_name, line_numbers in FILES:
file_path = f"{site_packages}/{file_name}.pyi"
with open(file_path, "r") as file_in:
file_lines = []
for index, line in enumerate(file_in.readlines()):
line_number = index + 1
if line_number in line_numbers and "ignore" not in line:
file_lines.append(
f"{line.rstrip()} # type: ignore[var-annotated]\n"
)
else:
file_lines.append(line)
with open(file_path, "w") as file_out:
file_out.writelines(file_lines)
FILES = [
("django-stubs/contrib/auth/base_user", (18, 19)),
(
"django-stubs/contrib/auth/models",
(23, 24, 25, 34, 35, 57, 58, 59, 70, 71, 72, 73, 74, 75, 76),
),
("django-stubs/contrib/sites/models", (17, 18)),
]
Not my best work, but I see enough value in the plugin that I wanted to get it to work.
Ok, I've made some progress.
I am now using this approach: https://github.com/dry-python/returns/blob/220ca059ad8e49ba25372023a381a0798eb351c7/setup.cfg#L151
P.S. @patrick91 this project looks amazing: https://github.com/strawberry-graphql/strawberry
I am going to share it in my https://t.me/opensource_findings tomorrow!
@sobolevn : I believe this works only for real libraries and not if you rely on third-party stubs (as I do with Django).
Still, progress!
But even then, you lose typing that you count on for your normal Mypy checks.
good point, I worked around that with a custom config just for the mypy tests ๐
I fixed it by adding this beauty to tests/conftest.pf:
Thanks for sharing, it might be useful in future!
P.S. @patrick91 this project looks amazing: strawberry-graphql/strawberry
I am going to share it in my t.me/opensource_findings tomorrow!
Thank you! Just joined ๐ Let me know if you try it!
I've forked this plugin to include an option --mypy-only-local-stub
. This will disable mypy from type-checking site-packages. Specifically, it disables the line below
Maybe this approach can help you. @silviogutierrez
I can create a PR if anyone is interested.