Unsound assignment of a method to a Callable
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Assigning functions to a name that was defined as a Callable is unsound and results in TypeErrors when run in Python.
To Reproduce
Just typecheck the code below with --strict:
class B():
a: Callable[['B', int], int]
def b(self, i: int) -> int: return 1
a = b
B().a(B(), 1)
results in no errors.
Expected Behavior
Python however raises a TypeError for the same code:
>>> B().a(B(), 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: b() takes 2 positional arguments but 3 were given
I feel like Mypy should probably somdisallow assigning functions in the class body to Callable
Your Environment
- Mypy version used: 9e1f4df133e155f213cf3714bf796bb9e8698907 (> v1.5.1)
- Mypy command-line flags: --strict
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.10
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es wird keine Quelldatei oder kein Test genannt. Führe zuerst das bereitgestellte Beispiel mit mypy --strict aus und vergleiche es mit Pythons TypeError; verfolge anschließend, wie Zuweisungen im Klassenrumpf zu Callable-Annotationen geprüft werden. Als erledigt gilt die Aufgabe, wenn die unsound assignment abgelehnt wird und ein Regressionstest das Beispiel abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 32/100