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.

mypy: invalid type comment or annotation

See original GitHub issue

Hi,

I think this issue related more to mypy, but maybe you know about any workaround or how to solve this.

Here is my code:

class Test(BaseModel):
    k: constr(min_length=2)

And when I run mypy I got:

error: invalid type comment or annotation
note: Suggestion: use constr[...] instead of constr(...)

Any ideas?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:42
  • Comments:38 (15 by maintainers)

github_iconTop GitHub Comments

31reactions
mattzquecommented, Apr 13, 2021

This should be mentioned in the documentation:

https://pydantic-docs.helpmanual.io/usage/types/#constrained-types

is incompatible with mypy at the moment

31reactions
m-laniakeacommented, Apr 16, 2020

The above is not quite right as well, because the first parameter to Field is the default value. In this case the default would be the type str, which is not a str.

Another possible issue is that a default value was supplied, making this field optional.

Updated example:

class User(BaseModel):
    name: str = Field('Default name', max_length=255, strip_whitespace=True)

If you want to make the field a Required field, use an ellipsis:

class User(BaseModel):
    name: str = Field(..., max_length=255, strip_whitespace=True)

Something like required=True would feel more natural, but that’s not an option as of v1.4. Please be aware that supplying a default of None is not the same as supplying no default value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mypy: invalid type comment or annotation · Issue #156 - GitHub
To anyone still running into this, # type: ignore now works. Its error code if you want to be pedantic (heh) is valid-type...
Read more >
mypy "invalid type" error - python - Stack Overflow
I'm trying to implement type annotations in a current project, and am receiving errors from mypy that I don't understand. I'm using Python...
Read more >
Common issues and solutions - mypy 0.991 documentation
A # type: ignore comment at the top of a module (before any statements, including imports or docstrings) has the effect of ignoring...
Read more >
mypy's response to that code: error:invalid type comment or ...
error:invalid type comment or annotation note:Suggestion: use intBit[...] instead of intBit(...) So what's really happening is the type ...
Read more >
python/mypy - Gitter
Is there a way to force mypy to ignore a line completely? # type: ignore does not suppress error: invalid type comment or...
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