Cannot anotate type of generic functions.
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 32/100
Direção de pesquisa
Comece pelos exemplos de funções genéricas e pela issue vinculada do Pyright e, em seguida, compare-os com as regras atuais de escopo de variáveis de tipo. Considera-se concluído quando a especificação de typing tiver uma resolução acordada para anotar variáveis de função genéricas e variáveis de instância de classe; a issue não menciona arquivos nem testes do repositório.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
The current scoping rules for type variables seems to deny type annotations over generic functions.
The issue
define two functions as follows:
def fx_1(f: Callable[[bool], int]) -> int:
return f(True)
def fx_2[T](f: Callable[[bool], T]) -> T:
return f(True)
If a function alias is required, these functions can be assigned to variables. However, if type annotations are used, fx_2 cannot be annotated using compliant type checkers (i.e. pyright).
T = TypeVar("T")
fx_1_alias: Callable[[Callable[[bool], int]], int] = fx_1 # OK!
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2 # complains that T has no meaning
Justification
- All variables should be able to be type annotated, even when those variables are functions or the annotation is not required.
defis a syntactic sugar equivalent to a variable assignation over a lambda function. As a result, it should be possible to define generic functions withoutdefstatements e.g.
fx_2_alternative: Callable[[Callable[[bool], T]], T] = lambda f: f(True)
- Generics are the first ladder of what is called a "dependent type". In the case of functions, this corresponds to a Π type where the input itself is the concrete type that will be replaced on the type var of a generic type. Here type-vars scope is over the function type itself rather than some outer entity (class, function, method).
- By these definitions of Π type, it cannot exist an unbound type variable when function/callable types are involved, since in python, the type argument is implicit and supplied when referring a TypeVar.
e.g.
T = TypeVar("T")
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2
fx_2_alias_2: Callable[[Callable[[bool], T]], T] = fx_2
Even when using the same type var fx_2_alias and fx_2_alias_2 each var is an independent one.
A more clear syntax (like the introduced on def statements on python312) could be
fx_2_alias: Callable[X][[Callable[[bool], X]], X] = fx_2
fx_2_alias_2: Callable[X][[Callable[[bool], X]], X] = fx_2
This marks explicitly that the scope of the var is over the type itself. However, this issue does not aim to propose a new syntax, but rather aims to adjust the scoping rules of type vars.
Motivation
This issue was derived from a pyright issue where generic functions as class instance variables are denied by the checker because of the same scope issues described before.
- Linguagem predominante
- Python
- Estrelas
- 1.8k
- Forks
- 302
- Merge médio
- 4h 16min
- PRs com merge (30d)
- 7
Preparar o ambiente
Este projeto não oferece contêiner de desenvolvimento, Dockerfile nem guia de contribuição, então a configuração fica por sua conta: comece pelo README e veja nosso guia da primeira contribuição para os passos gerais.
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/typing
-
topic: typing spec
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
python/typing#2356 · 2 comentários · 1 reação ·
Mantenedores costumam responder em até 1 dia
-
topic: typing spec
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
Mantenedores costumam responder em até 1 dia
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
python/typing#2227 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
python/typing#2150 · 2 comentários · 2 reações ·
Mantenedores costumam responder em até 1 dia
-
topic: conformance tests topic: typing spec
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 72/100
python/typing#2351 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
Todas as issues de python/typing
Issues semelhantes
-
tool-calling
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
vllm-project/vllm#59838 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
raullenchai/Rapid-MLX#4042 ·
Mantenedores costumam responder em até 1 dia
-
documentation
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
transitmatters/mbta-slow-zone-bot#70 ·
Mantenedores costumam responder em até 1 dia
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
litestar-org/advanced-alchemy#811 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
Mantenedores costumam responder em até 1 dia