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 crashes with pydantic plugin and BasicSettings class #18612

Closed
dimanu-py opened this issue Feb 5, 2025 · 4 comments
Closed

mypy crashes with pydantic plugin and BasicSettings class #18612

dimanu-py opened this issue Feb 5, 2025 · 4 comments
Labels
crash topic-plugins The plugin API and ideas for new plugins

Comments

@dimanu-py
Copy link

Crash Report

I've enabled pydantic mypy plugin as I've introduced a class in my project that uses BaseSettings class from pydantic.

Now when I run mypy I get the following error: AsertionError: All arguments mus be fully typed. As all fields are typed (see class in how to reproduced section), I don't understand where this error is coming from.

Traceback

src/shared/infra/settings.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.15.0
Traceback (most recent call last):
  File "mypy/semanal.py", line 7240, in accept
  File "mypy/nodes.py", line 1177, in accept
  File "mypy/semanal.py", line 1728, in visit_class_def
  File "mypy/semanal.py", line 1944, in analyze_class
  File "mypy/semanal.py", line 1991, in analyze_class_body_common
  File "mypy/semanal.py", line 2076, in apply_class_plugin_hooks
  File "/home/dmartinez/Developer/diego/social-network/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 159, in _pydantic_model_class_maker_callback
    transformer.transform()
    ~~~~~~~~~~~~~~~~~~~~~^^
  File "/home/dmartinez/Developer/diego/social-network/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 455, in transform
    self.add_initializer(fields, config, is_settings, is_root_model)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dmartinez/Developer/diego/social-network/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 866, in add_initializer
    add_method(self._api, self._cls, '__init__', args=args, return_type=NoneType())
    ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/dmartinez/Developer/diego/social-network/.venv/lib/python3.13/site-packages/pydantic/mypy.py", line 1257, in add_method
    assert arg.type_annotation, 'All arguments must be fully typed.'
           ^^^^^^^^^^^^^^^^^^^
AssertionError: All arguments must be fully typed.
src/shared/infra/settings.py:7: : note: use --pdb to drop into pdb

To Reproduce

In my mypy.ini file I've added the following:

plugins = pydantic.mypy

And the class that I created is this one:

class Settings(BaseSettings):
    model_config: ClassVar[SettingsConfigDict] = SettingsConfigDict(env_file=(".env", ".env.prod"), extra="ignore")
    postgres_user: str = Field(alias="POSTGRES_USER")
    postgres_password : str = Field(alias="POSTGRES_PASSWORD")
    postgres_db: str = Field(alias="POSTGRES_DB")
    postgres_host: str = Field(alias="POSTGRES_HOST")
    postgres_port: str = Field(alias="POSTGRES_PORT")

    @property
    def postgres_url(self) -> str:
        return f"postgresql://{self.postgres_user}:{self.postgres_password}@{self.postgres_host}:{self.postgres_port}/{self.postgres_db}"

Your Environment

  • Mypy version used: 1.15.0
  • Mypy command-line flags: no flags
  • Mypy configuration options from mypy.ini (and other config files):
this is my mypy.ini config file
[mypy]
files = src, tests
python_version = 3.13
mypy_path = .
disable_error_code = override,attr-defined
check_untyped_defs = true
disallow_any_explicit = false

# None and Optional handling
no_implicit_optional = true

# Configuring warnings
warn_redundant_casts = true
warn_unused_ignores = false
warn_no_return = true
warn_return_any = true
warn_unreachable = true

# Miscellaneous strictness flags
implicit_reexport = true
strict_equality = true

# Configuring error messages
show_error_context = true
show_column_numbers = true
show_error_codes = true
pretty = true
show_absolute_path = false

disallow_untyped_defs = true

plugins = pydantic.mypy

[mypy-expects.*]
ignore_missing_imports = True
[mypy-doublex.*]
ignore_missing_imports = True
[mypy-src.*]
ignore_missing_imports = True
[mypy-tests.*]
disallow_untyped_defs = False
[mypy-doublex_expects.*]
ignore_missing_imports = True
  • Python version used: 3.13
  • Operating system and version: Ubuntu 24
@dimanu-py dimanu-py added the crash label Feb 5, 2025
dimanu-py added a commit to dimanu-py/codenet that referenced this issue Feb 5, 2025
I did add pydantic.mypy plugin but the Settings class was giving an InternalError from the library. I've opened an [issue](python/mypy#18612) and removed the plugin until it worked
@sterliakov
Copy link
Collaborator

This sounds like an issue with pydantic plugin, not with mypy itself: the assertion comes from plugin code.

@sterliakov sterliakov added the topic-plugins The plugin API and ideas for new plugins label Feb 10, 2025
dimanu-py added a commit to dimanu-py/codenet that referenced this issue Feb 11, 2025
I did add pydantic.mypy plugin but the Settings class was giving an InternalError from the library. I've opened an [issue](python/mypy#18612) and removed the plugin until it worked
dimanu-py added a commit to dimanu-py/codenet that referenced this issue Feb 11, 2025
I did add pydantic.mypy plugin but the Settings class was giving an InternalError from the library. I've opened an [issue](python/mypy#18612) and removed the plugin until it worked
dimanu-py added a commit to dimanu-py/codenet that referenced this issue Feb 11, 2025
I did add pydantic.mypy plugin but the Settings class was giving an InternalError from the library. I've opened an [issue](python/mypy#18612) and removed the plugin until it worked
dimanu-py added a commit to dimanu-py/codenet that referenced this issue Feb 11, 2025
I did add pydantic.mypy plugin but the Settings class was giving an InternalError from the library. I've opened an [issue](python/mypy#18612) and removed the plugin until it worked
@srulre
Copy link

srulre commented Mar 2, 2025

I had a similar issue, but it seems to be working after upgrading to pydantic-settings 2.8.1

@dimanu-py
Copy link
Author

I had a similar issue, but it seems to be working after upgrading to pydantic-settings 2.8.1

I'll check it out, thanks for the update!!

@hauntsaninja
Copy link
Collaborator

Issues when running with the pydantic plugin are best reported at the pydantic repo

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

4 participants