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 1.5 thinks optional field is non-optional in a generic model #7280

Closed
1 task done
miikka opened this issue Aug 29, 2023 · 3 comments
Closed
1 task done

mypy 1.5 thinks optional field is non-optional in a generic model #7280

miikka opened this issue Aug 29, 2023 · 3 comments
Assignees
Labels
bug V2 Bug related to Pydantic V2 mypy related to mypy

Comments

@miikka
Copy link

miikka commented Aug 29, 2023

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

For the code below, mypy 1.5.0 and 1.5.1 give the following output:

pydantic_hmm.py:24: note: Revealed type is "pydantic_hmm.Data"
pydantic_hmm.py:25: error: Right operand of "and" is never evaluated  [unreachable]

This is not what I expected - the type should be Optional[Data]. When the test is run, it passes and the right-hand side of the assertion does get evaluated.

This works as expected with mypy 1.4.1. Here's its output:

pydantic_hmm.py:24: note: Revealed type is "Union[pydantic_hmm.Data, None]"

For what it's worth, Pylance gives the same revealed type as mypy 1.4.1.

I'm not sure if this is a Pydantic issue or a mypy issue, but I wasn't able to reproduce this without using BaseModel and pydantic.mypy mypy plugin so I'm reporting it here.

Example Code

from typing import TYPE_CHECKING, Generic, Optional, TypeVar

from pydantic import BaseModel


class Data(BaseModel):
    pass


T = TypeVar("T", bound=Data)


class BaseWrapper(BaseModel, Generic[T]):
    data: Optional[T] = None


class Wrapper(BaseWrapper[Data]):
    pass


def test_vrapped_data_is_none() -> None:
    wrapper = Wrapper()
    if TYPE_CHECKING:
        reveal_type(wrapper.data)
    assert wrapper.data is None and True

mypy.ini:

[mypy]
plugins = pydantic.mypy
warn_unreachable = True

Python, Pydantic & OS Version

pydantic version: 2.3.0
        pydantic-core version: 2.6.3
          pydantic-core build: profile=release pgo=false
                 install path: /Users/miikka.koskinen/Library/Caches/pypoetry/virtualenvs/xxx-mwympnZX-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.4 (main, Jun 20 2023, 16:59:59) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                     platform: macOS-13.5-x86_64-i386-64bit
     optional deps. installed: ['typing-extensions']


mypy 1.5.1 (compiled: yes)

Selected Assignee: @Kludex

@miikka miikka added bug V2 Bug related to Pydantic V2 unconfirmed Bug not yet confirmed as valid/applicable labels Aug 29, 2023
@miikka
Copy link
Author

miikka commented Oct 4, 2023

Checked and this is still an issue with Pydantic 2.4.2. To make reproduction easier, I made a small repo with a Poetry setup: https://github.com/miikka/repro_pydantic_7280

@Kludex Kludex added mypy related to mypy and removed unconfirmed Bug not yet confirmed as valid/applicable labels Oct 4, 2023
@Kludex Kludex removed their assignment Oct 4, 2023
@Kludex
Copy link
Member

Kludex commented Oct 4, 2023

Yeah, it does look like a bug. Thanks. 🙏

@miikka
Copy link
Author

miikka commented Dec 22, 2023

Just came to back to this ticket to see if I can do anything about it and it seems PR #7411 has fixed it!

Looks like this issue is essentially duplicate of #7399.

@Kludex Kludex closed this as completed Dec 22, 2023
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 mypy related to mypy
Projects
None yet
Development

No branches or pull requests

3 participants