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 pydantic plugin comparaison no error #8773

Closed
1 task done
tyki6 opened this issue Feb 9, 2024 · 2 comments · Fixed by #8799
Closed
1 task done

Mypy pydantic plugin comparaison no error #8773

tyki6 opened this issue Feb 9, 2024 · 2 comments · Fixed by #8799
Assignees
Labels
mypy related to mypy

Comments

@tyki6
Copy link

tyki6 commented Feb 9, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

With mpy pydantic plugin enable when i enable strict equality pydantic comparaison not generate error when i write mistake error

Example Code

[tool.mypy]
plugins = "pydantic.mypy"
ignore_missing_imports = true
warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
follow_imports = "normal"
show_column_numbers = true
strict_optional = true
warn_redundant_casts = true
pretty = false
strict = true
warn_unused_ignores = true
check_untyped_defs = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_untyped_decorators = false
strict_equality = true
[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = true
warn_required_dynamic_aliases = true


```python
from pydantic import BaseModel
class User(BaseModel):
    username: str
user = User(username="test")
print(user == "test") # no error
print(user.username == [1,2,3]) # no error
print(user.username == "test") 
mypy .

I got:

Success: no issues found

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=trueC
               python version: 3.12.1 (main, Dec  7 2023, 20:45:44) [GCC 11.4.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
             related packages: pydantic-settings-2.1.0 mypy-1.8.0 fastapi-0.109.0 typing_extensions-4.9.0 email-validator-2.1.0.post1
                       commit: unknown
@tyki6 tyki6 added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 9, 2024
@sydney-runkle sydney-runkle added mypy related to mypy and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 12, 2024
@sydney-runkle
Copy link
Member

@dmontagu,

Could you please take a look at this! Thanks :).

@dmontagu
Copy link
Contributor

@tyki6 what version of mypy are you using?

I checked both mypy v1.8.0 and v1.1.1 in both, I get:

/Users/davidmontague/Library/Application Support/JetBrains/PyCharm2023.3/scratches/scratch.py:14:7: error: Non-overlapping equality check (left operand type: "str", right operand type: "list[int]")  [comparison-overlap]

on the line with user.username == [1,2,3], which you seemed to mark as # no error. That said, I also get an error on the previous line if I change it to a dataclass, but not with BaseModel.

I suspect the reason for this is that we have defined __eq__ method on BaseModel with other as Any. If we hide that __eq__ from the type-checker using if not typing.TYPE_CHECKING, the issue goes away, so that may be the best fix, I'll run that by the other maintainers and if they agree we can merge it. (Another alternative may be to change the type hint on the other argument to __eq__ but I'm more concerned that changing that may affect other users.

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

Successfully merging a pull request may close this issue.

3 participants