stubgen creates duplicate class definitions

Aberta
#10,526 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
35/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
tooling

Direção de pesquisa

Comece com stubgen e seu tratamento das definições condicionais de plataforma em serialposix; o arquivo gerado stubs/serial/serialposix.pyi mostra as declarações duplicadas de PlatformSpecific. Determine como as definições de classes específicas da plataforma devem ser representadas e, em seguida, verifique se o stub gerado não produz mais nomes duplicados e se o mypy o aceita em todas as plataformas.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

bug topic-stubgen

I need a stub for the serial package, and attempted to use stubgen to create it. The problem arises in the serialposix subpackage, which defines the PlatformSpecific class differently depending upon the platform it's running on. When stubgen processes this code, it discards all the conditional pieces, the end result being multiple definitions of the PlatformSpecific class, which, not surprisingly, mypy is very unhappy about.

Here's what the original code looks like (trimmed for compactness):

plat = sys.platform.lower()
if plat[:5] == 'linux':    # Linux (confirmed)  # noqa
    class PlatformSpecific(PlatformSpecificBase):
        …
elif plat == 'cygwin':       # cygwin/win32 (confirmed)
    class PlatformSpecific(PlatformSpecificBase):
        …
elif plat[:6] == 'darwin':   # OS X
    class PlatformSpecific(PlatformSpecificBase):
        …

and so on. You get the picture.

Running mypy after generating the stub gives:

stubs/serial/serialposix.pyi:24: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:29: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:37: error: Name 'PlatformSpecific' already defined on line 20
stubs/serial/serialposix.pyi:42: error: Name 'PlatformSpecific' already defined on line 20

Now, I can manually go in and edit the generated stub, commenting out the class definitions for platforms other than the one I am running on. But the problem is that I can't then check in that stub and expect mypy to run correctly on other platforms.

I don't think this is a typeshed issue, because I don't see a way that the stub can be written to work across platforms, which seems to be a problem inherent in the way that stubgen, and stubs in general, work. Unless I'm missing something (which is entirely possible), there is nothing in mypy that will allow this type of code to be stubbed successfully.

Linguagem predominante
Python
Estrelas
20.6k
Forks
3.3k
Merge médio
1d 18h
PRs com merge (30d)
54

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/mypy

Todas as issues de python/mypy

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.