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
Declaring field types as sub-classes of base types gets highlighted in #312
Comments
What is unfortunate here, is that declaring fields as sub-classes (above) is the only to avoid |
The problem is not a bug. https://koxudaxi.github.io/pydantic-pycharm-plugin/type-checker-for-pydantic/ I put an example for working your code. project structure
pyproject.toml[tool.pydantic-pycharm-plugin.parsable-types]
# str field may parse int and float
str = ["int", "float"]
# datetime.datetime field may parse int
"datetime.datetime" = [ "int" ]
# your_module.your_type field may parse str
"your_app.main.MyStr" = [ "str" ]
[tool.pydantic-pycharm-plugin]
# You can set higlith level (default is "warning")
# You can select it from "warning", "weak_warning", "disable"
parsable-type-highlight = "warning"
## If you set acceptable-type-highlight then, you have to set it at same depth.
acceptable-type-highlight = "disable"
|
Uh, you're completely right (how else!). I did miss that bit in the docs. I just didn't expect that you have to configure the plugin for a basic use case. After adding It is kinda cumbersome having to maintain these mappings in a configuration file, don't you think? Have you considered a mode, where any |
I found something, though. Even if I whitelist from typing import Dict, Optional
from pydantic import BaseModel, StrictStr
class Str1(StrictStr):
pass
class InnerModel(BaseModel):
pass
class Model(BaseModel):
field1: Str1
field1_opt: Optional[Str1]
field2: Dict[str, int]
field2_opt: Optional[Dict[str, int]]
field3: Dict[Str1, int]
field3_opt: Optional[Dict[Str1, int]]
field4: Dict[Str1, InnerModel]
field4_opt: Optional[Dict[Str1, InnerModel]]
Model(
field1='fo',
field1_opt='fo',
field2={'bar': 2},
field2_opt={'bar': 2},
field3={'bar': 2},
field3_opt={'bar': 2}, # incorrectly highlighted
field4={'bar': InnerModel()},
field4_opt={'bar': InnerModel()}, # incorrectly highlighted
) Should I open another ticket about it? |
That looks great idea.
I guess it may a bug. |
Yes, that would be a great start. But it should also: class MyStr1(StrictStr): pass
class MyStr2(StrictStr): pass ...accept either of above as another, although they don't inherit each other. Considering that pydantic already maps the constrained types as base types when running type checking, simply doing the same with your plugin might be a viable option. So, if it could always resolve "base type" (e.g. I believe that is something like what will happen in pydantic v2 anyway: class Pokemon(BaseModel):
name: Annotated[str, Constraints(min_length=1)] = Field('pikachu', alias='Name') Here, you can just pick the
👍🏻 |
Some users may want to check strict types strictly. 🤔
This plugin already has supported |
Yeah, that's true and why not. It just gets tricky quickly and simply staying on top of base types is something that should work sufficiently without extra magic 🌟 So, I think
That's great. What I mean is that with class Model(BaseModel):
field: Annotated[str, Field(min_length=1)]
|
Hi, thanks for the great plugin!
Describe the bug
When using sub-classed
Constrained*
fields,Expected type '...', got '...' instead.
error is shown.See this example (using
StrictStr
which is inherited fromConstrainedStr
):It reproduces even when subclassing from base types, ie.
str
:To Reproduce
Above.
Expected behavior
There's shouldn't be error.
Screenshots
Uh, above.
Environments (please complete the following information):
The text was updated successfully, but these errors were encountered: