Skip to content

mypy plugin crash with NewType attribute override #12928

@chaosct

Description

@chaosct

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I encountered a crash in the mypy plugin. It happens in a particular situation when inheriting from a pydantic base class and overriding a member with a NewType. AFAIK the NewType has to be defined in another file and imported for it to crash.

You can find the minimal reproduction code here (as it needs multiple files): https://github.com/chaosct/pydantic_mypy_newtype_override

Then when running mypy:

uv run mypy consumer.py
Uninstalled 1 package in 76ms
Installed 1 package in 28ms
/Users/protopixel/Documents/projects/pydantic_mypy_newtype_override/reduced_case9/broken.py:5: 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.19.1
Traceback (most recent call last):
  File "mypy/semanal.py", line 7568, in accept
  File "mypy/nodes.py", line 1478, in accept
  File "mypy/semanal.py", line 1827, in visit_class_def
  File "mypy/semanal.py", line 2044, in analyze_class
  File "mypy/semanal.py", line 2091, in analyze_class_body_common
  File "mypy/semanal.py", line 2176, in apply_class_plugin_hooks
  File "/Users/protopixel/Documents/projects/pydantic_mypy_newtype_override/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 162, in _pydantic_model_class_maker_callback
    transformer.transform()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/protopixel/Documents/projects/pydantic_mypy_newtype_override/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 466, in transform
    fields, class_vars = self.collect_fields_and_class_vars(config, is_a_root_model)
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/protopixel/Documents/projects/pydantic_mypy_newtype_override/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 630, in collect_fields_and_class_vars
    self._api.fail(
    ~~~~~~~~~~~~~~^
        'BaseModel field may only be overridden by another field',
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        sym_node.node,
        ^^^^^^^^^^^^^^
    )
    ^
  File "mypy/semanal.py", line 7543, in fail
AttributeError: attribute 'column' of 'Context' undefined
/Users/protopixel/Documents/projects/pydantic_mypy_newtype_override/reduced_case9/broken.py:5: : note: use --pdb to drop into pdb

Interestingly enough, if we use Field in the derived class, it won't crash:

# changed in reduced_case9/broken.py
class Broken(Base):
    #  attrs: AttrDict = AttrDict({"x": True})  #  this was crashing
    attrs: AttrDict = Field(default_factory=lambda: AttrDict({"x": True}))

Then:

uv run mypy consumer.py
Success: no issues found in 1 source file

Example Code

Python, Pydantic & OS Version

pydantic version: 2.12.5
        pydantic-core version: 2.41.5
          pydantic-core build: profile=release pgo=false
               python version: 3.13.7 (main, Sep  2 2025, 14:05:52) [Clang 20.1.4 ]
                     platform: macOS-14.5-arm64-arm-64bit-Mach-O
             related packages: mypy-1.19.1 typing_extensions-4.15.0
                       commit: unknown

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2topic-mypy pluginRelated to the mypy plugin

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions