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 blows up on the exotic types that return types, e.g. conint

See original GitHub issue
from pydantic import BaseModel, conint

class MyPyProblem(BaseModel):
    big_int: conint(gt=1000, lt=1024) = None

mypy --verbose tests/test_pydantic_bug_report.py

LOG:  Mypy version 0.620
LOG:  Could not load cache for tests.test_pydantic_bug_report: .mypy_cache/3.6/tests/test_pydantic_bug_report.meta.json
LOG:  Metadata not found for tests.test_pydantic_bug_report
LOG:  Parsing tests/test_pydantic_bug_report.py (tests.test_pydantic_bug_report)
LOG:  Bailing due to parse errors
LOG:  Build finished in 0.001 seconds with 0 modules, and 2 errors
tests/test_pydantic_bug_report.py:4: error: invalid type comment or annotation
tests/test_pydantic_bug_report.py:4: note: Suggestion: use conint[...] instead of conint(...)

I tried both the recommendation of [] and aliasing it (e.g. BIG_INT=conint(gt=1000, lt=1024))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
samuelcolvincommented, Aug 1, 2018

This is the kind of thing I find very frustrating about mypy. There’s no more detail than Invalid type, what am I supposed to do with that?

This works:

from typing import cast
from pydantic import BaseModel, ConstrainedInt

class BigInt(ConstrainedInt):
    gt = 1000
    lt = 1024

class MyModel(BaseModel):
    foo: BigInt  # required
    bar: BigInt = cast(BigInt, 1001)  # with a default

m = MyModel(foo=1011)
print(m.dict())

You have to use cast(BigInt, 1001) to avoid mypy whinging that 1001 is not an instance of BigInt.

0reactions
paulloubetpreligenscommented, Jun 10, 2022

Something like this works for me : big_int: conint(gt=1000, lt=1024) = None # type: ignore

Read more comments on GitHub >

github_iconTop Results From Across the Web

MyPy blows up on the exotic types that return types, e.g. conint
There's no more detail than Invalid type , what am I supposed to do with that? This works: from typing import cast from...
Read more >
Changelog - pydantic
Data validation and settings management using Python type hints.
Read more >
weixin_39611930的博客_CSDN博客-领域博主
fix some use of `snippet` in `types.rs`. 2021-01-12 ... MyPy blows up on the exotic types that return types, e.g. conint. 2021-01-12 ...
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