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

Should not mark value as incorrect type if type of value is contained in class hierarchy of annotated type #624

Closed
chbndrhnns opened this issue Jan 6, 2023 · 2 comments

Comments

@chbndrhnns
Copy link

chbndrhnns commented Jan 6, 2023

Is your feature request related to a problem? Please describe.
In the last line, PyCharm issues a warning on passing a string.

However, the value will be coerced into MyStr without problems and thus should not be marked as problem here.
The model can be created no matter if I pass in MyStr("abc") or "abc".

import pydantic


class MyStr(str):
    ...


class M(pydantic.BaseModel):
    val: MyStr


def test_():
    assert M(val="abc")

Describe the solution you'd like
Compare the class hierachy of the annotated and the provided type. Do not mark a value as invalid if the type of the provided value is in the hierarchy of the annotated value.
Like in the example: MyStr inherits from str so passing a str is fine.

@chbndrhnns chbndrhnns changed the title Should not mark values as incorrect type if valid after coercing Should not mark value as incorrect type if type of value is contained in class hierarchy of annotated type Jan 7, 2023
@samuelcolvin
Copy link

Duplicate of #140.

@koxudaxi
Copy link
Owner

@chbndrhnns
We are planning the support the list in v2. pydantic/pydantic#4680
currently, we have a work around the feature
https://koxudaxi.github.io/pydantic-pycharm-plugin/type-checker-for-pydantic/
pydantic/pydantic#4680

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

3 participants