23

I have this code:

from pydantic import BaseModel, constr

DeptNumber = constr(min_length=6, max_length=6)

class MyStuff(BaseModel):
    dept: DeptNumber

ms = MyStuff(dept = "123456")

deptnr.py:6: error: Variable "deptnr.DeptNumber" is not valid as a type
deptnr.py:6: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases

The provided link doesn't seem to really address my problem (I'm not using Type).

This happens with or without this mypy.ini:

[mypy]
plugins = pydantic.mypy

[pydantic-mypy]
init_typed = true

Initially I also had that error in a Pydantic choice as below, but I got around that by using Python's Literal instead.

DIR = choice(["North", "East", "South", "West"])

What do I need to change to make mypy happy with my Pydantic constr?

4
  • DIR isn't a type. If pydantic doesn't have type stubs, you could either write type stubs yourself, or you could type it as Any. Commented Apr 8, 2021 at 18:05
  • Are you running the mypy plugin? Commented Apr 8, 2021 at 22:37
  • @SuperShoot Yes, it doesn't seem to make a difference, though. Commented Apr 9, 2021 at 20:10
  • 1
    take a look at typing.Literal Commented Apr 9, 2021 at 20:14

3 Answers 3

30

This incompatibility with mypy has been discussed in this Github issue https://github.com/samuelcolvin/pydantic/issues/156. Sadly, no concrete solution that uses constr and keeps mypy happy was found.

On Pydantic v1 note 1, instead of constr, you can subclass pydantic's ConstrainedStr, which offers the same configurations and options as constr, but without mypy complaining about type aliases.

from pydantic import BaseModel, ConstrainedStr

class DeptNumber(ConstrainedStr):
    min_length = 6
    max_length = 6

class MyStuff(BaseModel):
    dept: DeptNumber

ms = MyStuff(dept='123456')

The Constrained* classes are briefly mentioned in the Strict Types section of the docs. It is defined in pydantic/types.py and as you can see is basically the same as constr:

class ConstrainedStr(str):
    strip_whitespace = False
    to_lower = False
    min_length: OptionalInt = None
    max_length: OptionalInt = None
    curtail_length: OptionalInt = None
    regex: Optional[Pattern[str]] = None
    strict = False

    ...

Validation works the same:

Traceback (most recent call last):
  File "test-2.py", line 13, in <module>
    ms = MyStuff(dept='123456789')
  File "pydantic/main.py", line 406, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 1 validation error for MyStuff
dept
  ensure this value has at most 6 characters (type=value_error.any_str.max_length; limit_value=6)

note 1:
I haven't gotten a chance to work with Pydantic v2 yet, so I don't know if it still works on that version.
I can only guarantee that the answer works on v1.

Sign up to request clarification or add additional context in comments.

8 Comments

Sadly this still seems to result in a general type issue with pylance. :/
@Monkeyphant That's unfortunate. I don't use Pylance, and mypy != pylance, and the original question was for mypy, no mention of pylance. You might want to ask a separate question for pylance.
mypy is still complaining ;Argument "dept" to "MyStuff" has incompatible type "str"; expected "DeptNumber" [arg-type]. Do you a specific mypy configuration?
On pyproject.toml, I had to use init_typed=False to make it work in the section [too.pydantic-mypy].
This is the nicest workaround I've found for this problem yet, it's quite clean: github.com/koxudaxi/datamodel-code-generator/issues/…
|
12

In Pydantic V2, you can use the StringConstraints type along with Annotated:

from pydantic import stringConstraints
from typing import Annotated

DeptNumber = Annotated[
    str,
    StringConstraints(
        min_length=6,
        max_length=6,
    )
] 

Annotated makes sure that DeptNumber is a str type, while adding some functionality on top of it.

Comments

7

You can try to use Field from Pydantic :

from pydantic import BaseModel, Field

class MyStuff(BaseModel):
    dept: str = Field(..., min_length=6, max_length=6)

It seems working for me.

4 Comments

The Field function also allows a number of constraints to be added to the field pydantic-docs.helpmanual.io/usage/schema/#field-customisation
This does not work with strip_whitespace=True.
The "constr" is the correct way. Field don't works as expected.
Specifically for constr, this other answer appears to offer a more complete (if less "idiomatic") solution. Have tried with Pylance (but not mypy) and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.