Skip to content
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

Fix inheriting from generic @frozen attrs class #15700

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def _make_frozen(ctx: mypy.plugin.ClassDefContext, attributes: list[Attribute])
else:
# This variable belongs to a super class so create new Var so we
# can modify it.
var = Var(attribute.name, ctx.cls.info[attribute.name].type)
var = Var(attribute.name, attribute.init_type)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is problematic with converters, e.g.

def converter(s: str) -> int:
    return int(s)

@attrs.define
class C:
    x: int = attrs.field(converter=converter)

The attribute.init_type will be str here :(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, nm, by the time it's deserialized, it's int again (there's also converter_init_type).

var.info = ctx.cls.info
var._fullname = f"{ctx.cls.info.fullname}.{var.name}"
ctx.cls.info.names[var.name] = SymbolTableNode(MDEF, var)
Expand Down
19 changes: 19 additions & 0 deletions test-data/unit/check-plugin-attrs.test
Original file line number Diff line number Diff line change
Expand Up @@ -2250,3 +2250,22 @@ c = attrs.assoc(c, name=42) # E: Argument "name" to "assoc" of "C" has incompat

[builtins fixtures/plugin_attrs.pyi]
[typing fixtures/typing-medium.pyi]

[case testFrozenInheritFromGeneric]
from typing import Generic, TypeVar
from attrs import frozen

T = TypeVar('T')

@frozen
class A(Generic[T]):
x: T

@frozen
class B(A[int]):
pass

b = B(42)
reveal_type(b.x) # N: Revealed type is "builtins.int"

[builtins fixtures/plugin_attrs.pyi]