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

IDE does no longer warn for non-subscripable #951

Open
luckydonald opened this issue Jun 17, 2024 · 2 comments
Open

IDE does no longer warn for non-subscripable #951

luckydonald opened this issue Jun 17, 2024 · 2 comments

Comments

@luckydonald
Copy link

luckydonald commented Jun 17, 2024

Describe the bug
The IDE doesn't warn when there's a index/keyword lookup operation.

Take this example:

from pydantic import BaseModel

class SomeModel(BaseModel):
   foo: str
   
instance = SomeModel(foo='fooo')
y1 = instance['foo']

Notice how the IDE doesn't warn for the use of ['foo'].
However, the result is

TypeError: 'SomeModel' object is not subscriptable

To Reproduce
Steps to reproduce the behavior:

  1. Write the code above
  2. See that the IDE doesn't warn.
  3. Write the code in Expected Behavior
  4. they should perform similar.

Expected behavior
Compare that to how the IDE handles a normal class:

class NotAPydanticModel(object):
   foo: str
   
instance = NotAPydanticModel()
y2 = instance['foo']

Notice how the IDE complains about the use of ['foo'] here.

Screenshots
image
It marks only the second ['foo'] as error.

Environments (please complete the following information):

  • IDE: PyCharm Professional 2024.1.3
  • OS: Linux fedora 6.8.11-300.fc40.x86_64 [Feature] minimum support #1 SMP PREEMPT_DYNAMIC Mon May 27 14:53:33 UTC 2024 x86_64 GNU/Linux
  • Pydantic Version 2.7.4
  • Plugin version 0.4.13

Additional context
Mypy has this correctly, but so should the IDE.

@luckydonald
Copy link
Author

luckydonald commented Jun 17, 2024

My guess is, because of def __class_getitem__(…) (which seems to be for generics and stuff), the IDE thinks we'll wanna use the class' getitem, i.e. instance.__class__[…].
So the IDE should ignore pydantic's BaseModel.__class_getitem__ when checking for that.

@luckydonald luckydonald changed the title IDE doesn't warn for subscripable IDE does no longer warn for non-subscripable Jun 19, 2024
@UltimateLobster
Copy link

Encountered it as well when covnverting a TypedDict to a BaseModel. I missed to parts which still treated it as a typed dict because I didn't get any warning :(

I created my own class and implemented __class_getitem__. Pycharm understood it correctly and gave me the warnings.. It seems like a genuine bug in the plugin.

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

No branches or pull requests

2 participants