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

Mypy plugin crashes with explicit_package_bases set as true #5191

Closed
5 of 15 tasks
Tracked by #5192
marcelopgc opened this issue Mar 14, 2023 · 6 comments
Closed
5 of 15 tasks
Tracked by #5192

Mypy plugin crashes with explicit_package_bases set as true #5191

marcelopgc opened this issue Mar 14, 2023 · 6 comments
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable

Comments

@marcelopgc
Copy link

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

After updating mypy to v1.1.1, I added the following pyproject.toml config, as descripted in the documentation, to prevent mypy errors on classes with Optional attributes:

[tool.mypy]
strict = true
files = "src/app"
mypy_path = "src/"
namespace_packages = true
explicit_package_bases = true
follow_imports = "silent"
ignore_missing_imports = true
exclude = "(resources/.*)"

plugins = [
    "pydantic.mypy"
]

warn_redundant_casts = true
warn_unused_ignores = true
disallow_any_generics = true
check_untyped_defs = true
no_implicit_reexport = true

# for strict mypy: (this is the tricky one :-))
disallow_untyped_defs = true

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true
warn_untyped_fields = true

Using that config, I found an internal error while running mypy

src/app/models/stages.py:152: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.1.1
Traceback (most recent call last):
  File "mypy/semanal.py", line 6261, in accept
  File "mypy/nodes.py", line 1123, in accept
  File "mypy/semanal.py", line 1561, in visit_class_def
  File "mypy/semanal.py", line 1641, in analyze_class
  File "mypy/semanal.py", line 1668, in analyze_class_body_common
  File "mypy/semanal.py", line 1751, in apply_class_plugin_hooks
  File "pydantic/mypy.py", line 150, in pydantic.mypy.PydanticPlugin._pydantic_model_class_maker_callback
  File "pydantic/mypy.py", line 315, in pydantic.mypy.PydanticModelTransformer.transform
  File "pydantic/mypy.py", line 524, in pydantic.mypy.PydanticModelTransformer.set_frozen
AssertionError: 
src/app/models/stages.py:152: : note: use --pdb to drop into pdb

If remove the explicit_package_bases configuration, mypy is able to run without this setting.

mypy version:

 name         : mypy                              
 version      : 1.1.1                             
 description  : Optional static typing for Python 

dependencies
 - mypy-extensions >=1.0.0
 - tomli >=1.1.0
 - typing-extensions >=3.10

Example Code

No response

Python, Pydantic & OS Version

pydantic version: 1.10.6
pydantic compiled: True
install path: /workspace/.venv/lib/python3.9/site-packages/pydantic
python version: 3.9.16 (main, Jan 11 2023, 15:55:30)  [GCC 10.2.1 20210110]
platform: Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.31
optional deps. installed: ['typing-extensions']

Affected Components

@marcelopgc marcelopgc added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Mar 14, 2023
@samuelcolvin
Copy link
Member

See #5192

@dmontagu
Copy link
Contributor

dmontagu commented Mar 16, 2023

@marcelopgc I'm looking at this now — do you have example code you can share that causes this issue when you run mypy on it? At the very least I will make it produce an error that we can debug as opposed to crashing mypy, but if I can reproduce the issue I should be able to resolve it.

@dmontagu
Copy link
Contributor

dmontagu commented Mar 16, 2023

@marcelopgc if you can't easily share code that I could use to reproduce the failure — if you checkout the branch in the pull request #5208 and install it locally (pip install .) and run mypy with the plugin, and can share the error message you get, it may provide some insight.

@marcelopgc
Copy link
Author

@dmontagu here it is the link to the code that we were able to reproduce this error: https://github.com/marcelopgc/pydantic1.10.6_debug

@dmontagu
Copy link
Contributor

@marcelopgc thank you — I was able to figure out what was going on by analyzing that codebase. Turns out the circular imports were causing something called a PlaceholderNode to be produced which I hadn't seen before. #5208 should resolve this issue for you.

@dmontagu
Copy link
Contributor

The fix for this will be released in 1.10.7 today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

3 participants