Forward references to type variables behave strangely in fine grained

Open
#4,762 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
devtools

Research direction

Start with the fine-grained test case named testForwardTypeVar and reproduce the two runs using the a.py and b.py inputs in the report. Investigate how the forward reference to T is processed across runs; done means the diagnostics remain consistent or the intended handling of invalid forward references is documented and tested.

Written by the indexing model from the issue text.

Description

priority-2-low topic-fine-grained-incremental

This is similar to #4615

In the situation with a forward reference to type variable, first and second run in fine grained mode give different results:

[case testForwardTypeVar]
import a
[file a.py]
from typing import Generic, TypeVar
import b

def f() -> None:
    class C(Generic[T]): pass
    x: C[int]

T = TypeVar('T')
[file b.py]
y = 0
[file b.py.2]
y = ''
[out]
a.py:5: error: Free type variable expected in Generic[...]
a.py:6: error: "C" expects no type arguments, but 1 given
==

The errors don't appear in the second run. A possible solution is to allow forward references to type variables, although it is quite hard.

A general comment about fine grained mode is that it will be probably hard to support detection of invalid forward references (see for example https://github.com/python/mypy/issues/2400), the error would depend on the order of targets in file.

Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.