-
-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot get attrs.Converter() with takes_self pass through mypy #1378
Comments
|
I forgot to circle back here. After playing a bit more with converters and validators I figured that the solution is actually pretty simple and straightforward: def str2int(s: str|int, self_: attrs.AttrsInstance) -> int:
if not isinstance(self_, C):
raise TypeError(f"Converter does not know how to work with {type(self_)}")
# Now the type of `self_` is properly inferred as `C`
return int(s)
@attrs.define
class C:
x: int = attrs.field(
converter=attrs.Converter(str2int, takes_self=True)
) I am closing the issue. If anyone thinks this is worth documenting I can prepare a pull request for that. |
Also possible to use |
I've been playing with the
attrs.Converter()
but I am unable to get it type-checked with mypy when usingtakes_self
. Now I am not considering (the obvious)Unsupported converter, only named functions, types and lambdas are currently supported
error. What I am struggling with is how to annotate the self. The following code:Leads to the following error:
Is this just my own ignorance or an issue with attrs or mypy?
I am using:
The text was updated successfully, but these errors were encountered: