Will recursive or mutually recursive bounds on TypeVar be supported?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start with the recursive TypeVar example in example.py and review the linked typing discussion at issue 59. Compare it with the pymcts/tree.py example, then determine the expected behavior for TypedCons, IntCons, and the rejected BadCons case; done means recursive or mutually recursive bounds are either supported consistently or the limitation is clearly resolved.
Written by the indexing model from the issue text.
Description
Reading through the discussion at https://github.com/python/typing/issues/59, there was an example that had TypeVar bound that referenced itself. Is this intended to be eventually implemented? The actual PEP just has a bare Comparable.
A motivating example (from the Java world) is discussed here, in the context of their Enum. An example in Python:
from typing import Generic, Optional, Tuple, TypeVar
# Would like to write bound='TypedCons[T, V]' here, where T is self-referential
T = TypeVar('T', bound='TypedCons')
V = TypeVar('V')
class TypedCons(Generic[T, V]):
def __init__(self, value: V, cdr: Optional[T]=None) -> None:
self.value = value
self.cdr = cdr
def first_two(self) -> Tuple[T, T]:
# Fails without cast with "Incompatible return value type: expected Tuple[T`1, T`1], got Tuple[example.TypedCons[T`1, V`2], T`1]"
return self, self.cdr
class IntCons(TypedCons['IntCons', int]):
pass
# Recursive bound would reject this
class BadCons(TypedCons['IntCons', str]):
pass
If I try to write the recursive bound, I get:
example.py:3: error: Invalid type "example.T"
A similar example where this is needed is at https://github.com/smallnamespace/pymcts/blob/b5e1375e67983ef7b5baa2511c680074fd31b00c/pymcts/tree.py#L78 -- here we need to use the fact that N and Node are identical.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 54
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/mypy
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
documentation
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
bug topic-configuration topic-error-reporting
Difficulty 2/5 1-3 hours Newbie friendliness 68/100