Skip to content

UP007 problems with pydantic and from __future__ import annotations #5434

@Mr-Pepe

Description

@Mr-Pepe

Consider the following file, written for Python3.9+.

# tmp.py
from __future__ import annotations

from typing import Union

from pydantic import BaseModel

x: Union[int, str] = 5


class A(BaseModel):
    y: Union[int, str] = 5

Running ruff check tmp.py --target-version py39 --select UP --fix with Ruff version 0.0.275 yields:

from __future__ import annotations

from typing import Union

from pydantic import BaseModel

x: int | str = 5


class A(BaseModel):
    y: int | str = 5

Running the script results in the following error:

Traceback (most recent call last):
  File "/home/felipe/Projects/voraus-vtest/tmp.py", line 10, in <module>
    class A(BaseModel):
  File "pydantic/main.py", line 178, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/typing.py", line 400, in pydantic.typing.resolve_annotations
    
  File "/home/felipe/.pyenv/versions/3.9.13/lib/python3.9/typing.py", line 292, in _eval_type
    return t._evaluate(globalns, localns, recursive_guard)
  File "/home/felipe/.pyenv/versions/3.9.13/lib/python3.9/typing.py", line 554, in _evaluate
    eval(self.__forward_code__, globalns, localns),
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'type' and 'type'

The module-level x seems to work fine (because of the __future__ import?). Should Ruff maybe not change the type annotation if from __future__ import annotations is present? The type annotation might be evaluated in a different context where the import is not available.

Metadata

Metadata

Assignees

Labels

configurationRelated to settings and configuration

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions