Skip to content

Mypy plugin crashes with explicit_package_bases set as true #5191

@marcelopgc

Description

@marcelopgc

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.XpendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions