Support for alternative generic inference algorithms
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 5/5
- Tempo estimado
- Mais de uma semana
- Facilidade para iniciantes
- 25/100
Direção de pesquisa
Comece comparando o exemplo em Python com os comportamentos de inferência de TypeScript e Kotlin descritos na issue. Defina quais algoritmos alternativos de inferência ou anotações devem ser suportados e como se espera que os tipos de argumentos mistos, os tipos exatos e a inferência excluída se comportem; a conclusão exigiria um design acordado e a especificação de tipagem correspondente.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
I believe there is a use case for generic inference that doesn't get as wide as possible.
Look at this example of an assertion function, you would never want to do an assertion between two different types, but there is currently no way to type this:
from typing import TypeVar
T = TypeVar("T")
def assert_something(expected: T, actual: T) -> None:
...
assert_something(1, "") # no error, SUS alert!, T is inferred as `object`
Here are some behaviors from other languages
TypeScript
In Typescript generic inference is narrowed to type level types(not down to instance level types) and is never widened:
function assertSomething<T>(expected: T, actual: T): void { ... }
assertSomething(1, "") // Argument of type 'string' is not assignable to parameter of type 'number'.
Kotlin
Kotlin by default acts the same as Python, but there are annotations for changing the behavior of inference.
NoInfer will exclude that usage from inferring the type.
fun <T> assertSomething(expected: T, actual: @NoInfer T) { }
assertSomething(1, "") // Type mismatch: inferred type is String but Int was expected
Exact will require the type of the parameter is equal at a type level (Number != Int)
fun <T> foo(x: @Exact T) { }
foo<Number>(1) // Type mismatch. Required: Number, Found: Int
OnlyInputTypes will require a type annotation if there is any difference in types between the usages of the generic:
fun <@OnlyInputTypes T> doSomething(a: T, b: T) { }
val a = doSomething("a", "b")
val b = doSomething("a", 1) // Type inference failed. The value of the type parameter T should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
- Linguagem predominante
- Python
- Estrelas
- 1.8k
- Forks
- 302
- Merge médio
- 23h
- PRs com merge (30d)
- 8
Guia de contribuição
Nenhum guia de contribuição indexado para este repositório
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
-
topic: typing spec
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
topic: documentation
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
-
topic: conformance tests topic: typing spec
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 72/100
Todas as issues de python/typing
Issues semelhantes
-
sponsored
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
Diaoul/subliminal#1382 ·
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
-
triage/confirmed
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 88/100
agentscope-ai/agentscope#2775 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100