Unsoundness with `typing.IO` and friends
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
- 25/100
Direção de pesquisa
Comece revisando o comportamento em tempo de execução de typing.IO, typing.BinaryIO, typing.TextIO e io.BytesIO, juntamente com suas relações no typeshed e a issue vinculada do CPython. Compare as três abordagens propostas e documente ou implemente uma especificação e uma direção de runtime acordadas; para considerar o trabalho concluído, é necessário resolver como isinstance() e issubclass() devem se comportar.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
The classes typing.IO, typing.BinaryIO, and typing.TextIO look like they want to be Protocols or ABCs, but in fact they are defined as regular generic classes, both at runtime and in typeshed. However, they are meant to encompass the concrete IO classes defined in the io module, and in typeshed we implement that by having these classes inherit from typing.*IO classes, even though there is no such inheritance at runtime.
This can easily lead to unsound behavior:
import io, typing
def f(x: int | io.BytesIO) -> int:
if isinstance(x, typing.BinaryIO):
return x.fileno()
return x
f(io.BytesIO()) + 1 # boom
Type checkers think BytesIO is a subclass of BinaryIO, because that's how it's defined in typeshed, but in fact it isn't at runtime.
I can see a few solutions:
- Special-case
typing.*IOin the spec and say that type checkers should rejectisinstance()/issubclass()calls involving them. - Deprecate the
typing.*IOclasses and eventually remove them, nudging people to use their own Protocols (or the newio.Reader/io.Writer) instead. This is conceptually clean but may be annoying for a lot of users; the typing classes are nice to use in simple application code. - Make these classes actually (runtime-checkable?) Protocols at runtime, though they would be unwieldily large.
Even if we do (2) or (3) type checkers might still want to do (1) since it will be a while before the relevant runtime changes take effect.
(Noticed this while looking into https://github.com/python/cpython/issues/133492 .)
- Linguagem predominante
- Python
- Estrelas
- 1.8k
- Forks
- 302
- Merge médio
- 23h
- PRs com merge (30d)
- 8
Preparar o ambiente
Ainda não verificamos os arquivos de configuração deste projeto. 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
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 74/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100
gradio-app/gradio#13895 ·
Mantenedores costumam responder em até 1 dia
-
build-error
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
spack/spack-packages#6713 ·
Mantenedores costumam responder em até 1 dia
-
Use issue templatesAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
ActivityWatch/activitywatch#1464 · 1 reação ·
Mantenedores costumam responder em até 1 dia
-
[Bug]: The ckg tool drops the return type of every decorated Python method in class search resultsAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
bytedance/trae-agent#483 ·
Mantenedores costumam responder em até 1 dia