microsoft / microsoft/pyright

pyright ignores from __future__ import annotations when it comes to method definitions

Open
#11,236 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**Description**

This code works in Python 3.12, it even passed `mypy` validation, but fails with `pyright`:

```python
from __future__ import annotations

from typing import Annotated

from pydantic import BeforeValidator

type MyStr = Annotated[str, BeforeValidator(_my_before_validator)]

def _my_before_validator(value: str) -> str:
return value
```

The error message is:

```
error: "_my_before_validator" is not defined (reportUndefinedVariable)
```

**Workaround:**

```python
type MyStr = Annotated[str, BeforeValidator(lambda v: _my_before_validator(v))] # pylint: disable=unnecessary-lambda
```

Contributor guide

Open the contributing guide

Research direction

No repository file or test is named in the report. Start by running the provided snippet through Pyright and compare its result with Python 3.12 and mypy, then trace analysis of future annotations, type aliases, and Annotated metadata. Done means the reproduction no longer reports _my_before_validator as undefined and a regression test covers it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.