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

Checking type with Enum, HttpUrl, conlist #99

Closed
hard-coders opened this issue Feb 19, 2020 · 3 comments
Closed

Checking type with Enum, HttpUrl, conlist #99

hard-coders opened this issue Feb 19, 2020 · 3 comments

Comments

@hard-coders
Copy link

I love your awesome plugin. However, there is some problem like below.

image

In the picture, number, url, urls are maybe wrong but con_urls is correct when instantiating a Bar. Hmm...Let's run the code

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    bar = Bar(
  File "/Users/spike/works/catalog/venv/lib/python3.8/site-packages/pydantic/main.py", line 283, in __init__
    raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for Bar
con_urls
  ensure this value has at most 1 items (type=value_error.list.max_items; limit_value=1)

Truth is only con_urls is incorrect. pydantic said the opposite of what plugin says. In addition, the plugin doesn't check those item's type and quantity at all!

I have an opinion. First, the member of Foo, one is driven from str and Enum both.

>>> isinstance(Foo, str)
True
>>> Foo.one == 'one'
True

So it is really okay to assign directly but the plugin show warning message: Expected type 'Foo', got 'str' instead.

Some people may think Foo is not str but Foo itself and should code with Foo.one. Okay, maybe they are right. How about HttpUrl?

>>> url = HttpUrl(url='http://example.com', scheme='http', host='example.com')
>>> isinstance(url, str)
True
>>> url == 'http://example.com'
True

I got it! It's the answer! And I decided to ignore conlist! Problem solved!

image

If someone says I have to code like above to avoid the problem, I will never use pydantic. Remember, the problem belongs to the plugin, not pydantic. I just want to type url directly without HttpUrl. BTW, instantiating HttpUrl is sucks.

I hope the problem fixed soon.

@koxudaxi
Copy link
Owner

@hard-coders
Thank you for creating this issue. And sorry for replying so late.

I think your question has two factors.

  1. The plugin doesn't care about casting type.
  2. PyCharm doesn't support to treat generic return type of function.

We talked about 1 for a long time. The plugin doesn't cast any type to check type.
#36

You can disable type-checking for init signature on the plugin. If you don't like the warning.
#92

However, this way is not user-friendly.
We want to cast type to some type (I call this feature is virtual cast)
I'm developing this feature. I expect to pass the casting type map to the plugin by a config file.
ex: int -> int, float, str
#96

But, I recently can't spend time developing the feature 🤦‍♂️

  1. is not a bug on the plugin. The Pycharm does not support this case.
    (conlist is a function to create a new type.)
    We may implement this feature to treat this case on the plugin.
    But, It maybe diffcult.
    Screenshot_2020-02-25_01-30-41

@koxudaxi
Copy link
Owner

@hard-coders
I have implemented some features for your case.
You can disable the warnings.
also, the plugin can parse return type of conlist.

the detail of settings is here.
https://koxudaxi.github.io/pydantic-pycharm-plugin/type-checker-for-pydantic/
pyproject.toml

[tool.pydantic-pycharm-plugin.acceptable-types]

"main.Foo" = ["str"]
"pydantic.HttpUrl" = ["str"]

[tool.pydantic-pycharm-plugin]
# You can set higlith level (default is "weak_warning")
# You can select it from "warning",  "weak_warning", "disable"
acceptable-type-highlight = "disable"

# If you set parsable-type-highlight then, you have to set it at the same depth.
parsable-type-highlight = "warning"

screenshot34

@hard-coders
Copy link
Author

@koxudaxi That's great! and I really thank you so much!

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