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 when trying to parse a pydantic model with optional fields #8665

Closed
1 task done
Tracked by #8671
kikones34 opened this issue Jan 29, 2024 · 3 comments · Fixed by #8666
Closed
1 task done
Tracked by #8671

Mypy crashes when trying to parse a pydantic model with optional fields #8665

kikones34 opened this issue Jan 29, 2024 · 3 comments · Fixed by #8666
Assignees
Labels
bug V2 Bug related to Pydantic V2

Comments

@kikones34
Copy link

kikones34 commented Jan 29, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I'm reporting the issue here because I think it's a problem with pydantic's mypy plugin. It was working fine before updating, more specifically it worked with pydantic-core==2.14.6 and pydantic==2.5.3.

Now, the error occurs only when both of these settings are specified in pyproject.toml:

[tool.mypy]
no_strict_optional = true
plugins = "pydantic.mypy"

When trying to parse a pydantic model with optional fields, mypy crashes as follows:

testo.py:4: 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.8.0
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "mypy\semanal.py", line 6539, in accept
  File "mypy\nodes.py", line 1142, in accept
  File "mypy\semanal.py", line 1612, in visit_class_def
  File "mypy\semanal.py", line 1697, in analyze_class
  File "mypy\semanal.py", line 1731, in analyze_class_body_common
  File "mypy\semanal.py", line 1816, in apply_class_plugin_hooks
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 187, in _pydantic_model_class_maker_callback
    return transformer.transform()
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 478, in transform
    self.add_initializer(fields, config, is_settings, is_root_model)
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 856, in add_initializer
    args = self.get_field_arguments(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 1039, in get_field_arguments
    field.to_argument(
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 346, in to_argument
    variable=self.to_var(current_info, use_alias),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 373, in to_var
    return Var(name, self.expand_type(current_info))
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 362, in expand_type
    raise _DeferAnalysis()
_DeferAnalysis:

Example Code

from pydantic import BaseModel


class MongoSettings(BaseModel):
    MONGO_PASSWORD: str | None

Python, Pydantic & OS Version

             pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/k34/repos/internal/trio-cloud-search/venv2/lib/python3.12/site-packages/pydantic
               python version: 3.12.1 (main, Dec 10 2023, 15:07:36) [GCC 11.4.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: mypy-1.8.0 typing_extensions-4.9.0
                       commit: unknown
@kikones34 kikones34 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Jan 29, 2024
@dmontagu
Copy link
Contributor

@kikones34 thanks for reporting, I've opened a PR that I believe fixes it — #8666. It would be great if you could confirm whether that resolves your issues.

@dmontagu
Copy link
Contributor

Yep, this was introduced in #7411 and it's obvious that the change I made there was not handling all possible scenarios under which the _DeferAnalysis error I introduced could be raised, but also that #8666 fixes that.

@sydney-runkle
Copy link
Member

@kikones34,

Thanks for bringing this to our attention. We'll do a patch release tomorrow including @dmontagu's fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants