Overloaded `__init__` prevents type from being recognized as an attrs class
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 48/100
Piste de recherche
Commencez dans mypy/plugins/attrs.py, autour des lignes 1033-1034, et reproduisez le problème avec le snippet fourni contenant un init surchargé en utilisant mypy 1.15.0. Le travail est terminé lorsque attrs.evolve accepte l’instance C sans l’erreur de type incompatible signalée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Bug Report
When an attrs class has and overloaded __init__ function, mypy does not consider it to be an attrs class when evaluating attrs.evolve.
To Reproduce
from typing import overload
import attrs
@attrs.frozen(init=False)
class C:
x: int | str
@overload
def __init__(self, x: int) -> None: ...
@overload
def __init__(self, x: str) -> None: ...
def __init__(self, x: int | str) -> None:
self.__attrs_init__(x)
obj = C(1)
attrs.evolve(obj, x=2) # error
Expected Behavior
The C instance should be an acceptable input to attrs.evolve.
Actual Behavior
$ mypy t.py
t.py:21: error: Argument 1 to "evolve" has incompatible type "C"; expected an attrs class [misc]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
mypy 1.15.0 - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used:
Python 3.13.0 (v3.13.0:60403a5409f, Oct 7 2024, 00:37:40) [Clang 15.0.0 (clang-1500.3.9.4)]
Notes
This appears to be because the init method is both not a FuncDef (it is OverloadedFuncDef) and its type attribute is not a CallableType. See: https://github.com/python/mypy/blob/7b4f6311e29452cc8d4ddb78331d0047c8b17e93/mypy/plugins/attrs.py#L1033-L1034
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Merge moyen
- 1 j 18 h
- PR mergées (30 j)
- 54
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de python/mypy
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
bug topic-configuration topic-error-reporting
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Toutes les issues de python/mypy
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
bancolombia/sentinel#23 ·
-
test md OuverteCI
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100