question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Declaring field types as sub-classes of base types gets highlighted in

See original GitHub issue

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 from ConstrainedStr):

image

It reproduces even when subclassing from base types, ie. str:

import pydantic

class MyStr(str):
    pass

class MyModel(pydantic.BaseModel):
    field: MyStr

MyModel(field='')

To Reproduce

Above.

Expected behavior

There’s shouldn’t be error.

Screenshots

Uh, above.

Environments (please complete the following information):

  • IDE: PyCharm Professional 2021.1
  • OS: Kubuntu 20.04 LTS
  • Pydantic Version: 1.8.2
  • Plugin version: 0.3.3

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
koxudaxicommented, Jun 1, 2021

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.

Some users may want to check strict types strictly. 🤔 This option will cover all case.

[tool.pydantic-pycharm-plugin]
constrained-type = "strict"  # or "lenient"

This plugin already has supported Annotated 😉

0reactions
tuukkamustonencommented, Jun 1, 2021

Some users may want to check strict types strictly.

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 constrained-type = "lenient" mode should be a good workaround/escape mechanism when all else fails 😄

This plugin already has supported Annotated

That’s great. What I mean is that with Annotated we don’t need this constrained-type = "lenient" logic at all, because with Annotated you always use the base type as the first argument, like:

class Model(BaseModel):
    field: Annotated[str, Field(min_length=1)]

Constrained* classes and con*() methods are not supported with Annotated, so there are no sub-types to worry about. But that also means that strict types are not supported now with Annotated 😢 (as there’s no Field(strict=...)) so it’s sensible to support also non-Annotated declarations, like you’re about to do here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5.1 Configuring Business Objects with Agile PLM Classes
Object types are organized by the hierarchy of Base Class (a kind of template), Class (more detailed, generally specific to a PLM solution),...
Read more >
11.15. Polymorphism — AP CSA Java Review - Obsolete
In Java an object variable has both a declared type or compile-time type and a run-time type or actual type. The declared type...
Read more >
Question concerning types, Maps and subclasses in typescript
Firstly, it seems like you want your Chart class to be abstract since you are only ever going to use concrete subclasses of...
Read more >
Kinds of types - mypy 0.991 documentation
Declared (and inferred) types are ignored (or erased) at runtime. They are basically treated as comments, and thus the above code does not...
Read more >
How To Use Classes in TypeScript | DigitalOcean
In the highlighted code, you added a parameter called name of type string to your class constructor. Then, when creating a new instance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found