python / python/mypy

False positive comparison-overlap for nested function and nested class

Aperta
#15,083 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Nested functions inside methods incorrectly resolve attributes of their first argument relative to the surrounding class.

To Reproduce

class foo:
    a: str = "foo"
    
class bar:
    class a:
        pass
    
    def test(self) -> None:
        def inner1(f: foo, s: str) -> bool:
            return f.a == s
        
        def inner2(s: str, f: foo) -> bool:
            return f.a == s

https://mypy-play.net/?mypy=latest&python=3.11&flags=strict&gist=4c27f653074cd1c3a48bb16f49ca2168

Expected Behavior

As far as I can tell this should be valid code, even though this rarely happens in practice due to CamelCase vs snake_case naming conventions.

Actual Behavior

inner1 fails to type-check, because the a attribute is incorrectly assumed to be the a class inside bar

main.py:10: error: Non-overlapping equality check (left operand type: "Type[a]", right operand type: "str")  [comparison-overlap]

Interestingly, inner2 is fine.

Your Environment

  • Mypy version used: 1.2.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Riproduci l’esempio dell’issue in mypy con Python 3.11 e --strict, quindi traccia il modo in cui le funzioni annidate risolvono l’accesso all’attributo nel confronto. La correzione è completata quando inner1 non segnala più un errore comparison-overlap, mentre inner2 rimane valido e la regressione è coperta da un test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.