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

INTERNAL ERROR: AssertionError: ClassDef is lacking info #15804

Closed
martinky24 opened this issue Aug 2, 2023 · 6 comments
Closed

INTERNAL ERROR: AssertionError: ClassDef is lacking info #15804

martinky24 opened this issue Aug 2, 2023 · 6 comments
Labels

Comments

@martinky24
Copy link

martinky24 commented Aug 2, 2023

Crash Report

I am seeing a Mypy INTERNAL ERROR on both 1.4.1 and 1.6.0+dev.54bc37ccade0476a1738b33cd34b6eb35d7124e1.

I don't see it on first run of mypy immedaitely after clearing .mypy_cache, but I see it on all runs after that first one.

Relatively minimal info to reproduce, at least for me:

This python file, config.py:

from typing import Any, Optional

from pydantic import PostgresDsn, field_validator
from pydantic_settings import BaseSettings


class Settings(BaseSettings):

    SQLALCHEMY_DATABASE_URI: Optional[PostgresDsn] = None

    @field_validator("SQLALCHEMY_DATABASE_URI", mode="before")
    def assemble_db_connection(cls, v: Optional[str], values: dict[str, Any]) -> Any:
        return v

This mypy configs file:

[tool.mypy]
python_version = "3.11"
plugins = ["pydantic.mypy"]

With these dependencies:

$ pip freeze
annotated-types==0.5.0
mypy @ file:///Users/kyle/open-source/mypy
mypy-extensions==1.0.0
pydantic==2.1.1
pydantic-settings==2.0.2
pydantic_core==2.4.0
python-dotenv==1.0.0
typing_extensions==4.7.1

This command:

mypy --config pyproject.toml --show-traceback config.py

Traceback

$ mypy --config pyproject.toml --show-traceback config.py
config.py:7: 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.6.0+dev.54bc37ccade0476a1738b33cd34b6eb35d7124e1
Traceback (most recent call last):
  File "/Users/kyle/open-source/mypy-crash/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/main.py", line 94, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/main.py", line 173, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/build.py", line 195, in build
    result = _build(
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager, stdout)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/build.py", line 2927, in dispatch
    process_graph(graph, manager)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/build.py", line 3325, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/build.py", line 3420, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 93, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 220, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal_main.py", line 349, in semantic_analyze_target
    analyzer.refresh_partial(
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 600, in refresh_partial
    self.refresh_top_level(node)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 611, in refresh_top_level
    self.accept(d)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 6465, in accept
    node.accept(self)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/nodes.py", line 1141, in accept
    return visitor.visit_class_def(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 1600, in visit_class_def
    self.analyze_class(defn)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 1685, in analyze_class
    self.analyze_class_body_common(defn)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 1714, in analyze_class_body_common
    self.apply_class_plugin_hooks(defn)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/semanal.py", line 1801, in apply_class_plugin_hooks
    hook(ClassDefContext(defn, base_expr, self))
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/pydantic/mypy.py", line 182, in _pydantic_model_class_maker_callback
    return transformer.transform()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/pydantic/mypy.py", line 443, in transform
    self.add_initializer(fields, config, is_settings)
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/pydantic/mypy.py", line 804, in add_initializer
    base_settings_init_arguments = base_settings_info.names['__init__'].node.arguments
                                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/kyle/open-source/mypy-crash/.venv/lib/python3.11/site-packages/mypy/nodes.py", line 3409, in __getattribute__
    raise AssertionError(object.__getattribute__(self, "msg"))
AssertionError: ClassDef is lacking info
config.py:7: : note: use --pdb to drop into pdb

To Reproduce

Above setup, with

mypy --config pyproject.toml --show-traceback config.py

Your Environment

  • Mypy version used: 1.6.0+dev.54bc37ccade0476a1738b33cd34b6eb35d7124e1 AND 1.4.1
  • Mypy command-line flags: mypy --config pyproject.toml --show-traceback config.py
  • Mypy configuration options:
[tool.mypy]
python_version = "3.11"
plugins = ["pydantic.mypy"]
  • Python version used: Python 3.11.4
  • Operating system and version: macOS Ventura 13.4.1
@martinky24
Copy link
Author

Appreciate all the work from the mypy team, if there's anything else I can provide let me know! Sorry if this is a dup.

@martinky24
Copy link
Author

I think this has something to do with Pydantic 2.0's field_validator

@JelleZijlstra
Copy link
Member

Chances are this is a bug in pydantic's mypy plugin, which is maintained by the Pydantic project, not by mypy itself.

@A5rocks
Copy link
Contributor

A5rocks commented Aug 5, 2023

Hi, maybe this is an instance of pydantic/pydantic#6985? While the manifested error is different it's the exact same code path in the pydantic plugin...?

@martinky24
Copy link
Author

I think the MR linked in that issue (pydantic/pydantic#7002) does fix this, from some local testing I did.

@JelleZijlstra
Copy link
Member

Closing as this is a bug in pydantic, not mypy.

@JelleZijlstra JelleZijlstra closed this as not planned Won't fix, can't repro, duplicate, stale Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants