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

Custom Fields not recognised by PyCharm #87

Closed
avlahop opened this issue Nov 22, 2019 · 5 comments
Closed

Custom Fields not recognised by PyCharm #87

avlahop opened this issue Nov 22, 2019 · 5 comments

Comments

@avlahop
Copy link

avlahop commented Nov 22, 2019

I have the following custom Field:

class NonEmptyString(str):
     @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(cls, v):
        if v == "":
            raise ValueError("Non-empty string expected")
        return v

and a model using it:

class MyModel(BaseModel):
    field: NonEmptyString

but when I instantiate MyModel:

my_model = MyModel(field="a string")

I get the following warning from pycharm's linter:

Expected type 'NonEmptyString' got 'str' instead

Is this behaviour expected?

Versions
pydantic 1.1.1
pycharm-pydantic-plugin: 0.0.27

@dmontagu
Copy link
Contributor

dmontagu commented Nov 22, 2019

I think this is expected; the only alternative would be to drop the type-checking for the __init__ entirely.

This has been discussed before, but I think it makes sense to make this a plugin config setting. Alternatively, the __init__ types could all be Any, and the plugin could generate a typed signature for BaseModel.construct, but that won't apply any of the validators, so anywhere you are using __init__ for meaningful logic, you'd lose type hints.

@koxudaxi Maybe we could add a pycharm-plugin-specific model Config setting (wouldn't need to be an official part of pydantic at all) that would let you enable/disable a typed init on a model-by-model basis? Something like pycharm_typed_init = False (or True)? That might make it easier to selectively disable warnings.

It would be very easy to add similar behavior to the mypy plugin...

@koxudaxi
Copy link
Owner

@avlahop

Is this behaviour expected?

Yes, We expect the behavior.
@dmontagu explained about the behavior and history.

We should add a panel of settings to control the warnings.
Sorry, I have not tried to implement the feature yet.
I will try it next week.

@dmontagu
Your idea looks good. But I try to implement the panel of settings.
If I can not do it next week, then let's discuss the option on `config.

@otsuka
Copy link

otsuka commented Dec 24, 2019

FYI,
pydantic EmailStr has a bug.

EmailStr type is used in a model,

addr = Address(email='foo@example.com')

the plugin raises a warning on editor.

addr = Address(email=EmailStr('foo@example.com'))

This code is type checking safe, but running it on Linux raises ValidationError.
pydantic/pydantic#1070 (comment)

@koxudaxi
Copy link
Owner

@avlahop

I write a workaround.
I add a new type-checker in the plugin.
You can set parsable-type and acceptable-type. there are details in this document page

If you want to remove warning then, you should change acceptable-type-highlight to "disable"

Screenshot_2020-04-28_23-41-38

pyproject.toml:

[tool.pydantic-pycharm-plugin]
# You can select higlith level  from "warning",  "weak_warning", "disable"
parsable-type-highlight = "warning"
acceptable-type-highlight = "weak_warning"
[tool.pydantic-pycharm-plugin.acceptable-types]
"main.NonEmptyString" = ["str"]

@koxudaxi
Copy link
Owner

I think the workaround is good.
If someone would discuss the issue then, I will re-open the issue.

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

4 participants