Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3k
Fix/attrs init overload#19104
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
uko3211 wants to merge29 commits intopython:masterChoose a base branch fromuko3211:fix/attrs-init-overload
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Fix/attrs init overload#19104
Changes from13 commits
Commits
Show all changes
29 commits Select commitHold shift + click to select a range
0ecd910
Fix handling of overloaded __init__ methods in attrs plugin
uko3211f6c8dfd
Fix: handle CallableType resolution for attrs __init__
uko3211989c9f3
Fix: Add exception handling and resolve issues caused by AnyType
uko3211c375805
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]58a1d3d
Fix: Add exception handling and resolve issues caused by AnyType and …
uko321122c8dfd
Fix: return Anytype
uko3211166776b
Fix: return Anytype on _get_expanded_attr_types
uko321104766f7
Add unit tests to verify fix for #19003 involving overloaded __init__…
uko32113b725e9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]34e831b
Add unit tests to verify fix for #19003 involving overloaded __init__…
uko32119065f79
Added error test case for overloaded custom __init__
uko3211bd929fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]ad415cb
Added error test case for overloaded custom __init__
uko321193db908
add plugin_generated check
uko3211c4716de
fix test case
uko32110d0e8d3
fix/check-plugin-attrs.test&attrs.py_2\
uko321183feade
add attrs.py function
uko32116382550
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]b82db50
change getattr -> .plugin_generated && fix nodes.py
uko3211b6c0b1a
edit check-plugin-attrs.test checking error
uko3211e1821c6
edit check-plugin-attrs.test error fix
uko321120c8ab5
Changes have been made based on the feedback
uko3211ae37831
Changes have been made based on the feedback and fixed mistake
uko32116a45089
Merge branch 'master' into fix/attrs-init-overload
uko32117f8d33c
Fixed an issue where overly permissive Any return types were masking …
uko32112946878
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]eba288b
Fixed an issue where overly permissive Any return types were masking …
uko32112bb782c
Update check-plugin-attrs.test file
uko3211423e526
Revised version of attrs.py based on the feedback
uko3211File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
15 changes: 12 additions & 3 deletionsmypy/plugins/attrs.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1022,17 +1022,24 @@ def add_method( | ||
) | ||
def _get_attrs_init_type(typ: Instance) -> CallableType | None | AnyType: | ||
""" | ||
If `typ` refers to an attrs class, get the type of its initializer method. | ||
""" | ||
magic_attr = typ.type.get(MAGIC_ATTR_NAME) | ||
if magic_attr is None or not magic_attr.plugin_generated: | ||
return None | ||
init_method = typ.type.get_method("__init__") or typ.type.get_method(ATTRS_INIT_NAME) | ||
if init_method is None: | ||
return None | ||
# case 1: normal FuncDef | ||
if isinstance(init_method, FuncDef) and isinstance(init_method.type, CallableType): | ||
uko3211 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return init_method.type | ||
# case 2: overloaded method | ||
if isinstance(init_method, OverloadedFuncDef) and isinstance(init_method.type, Overloaded): | ||
return AnyType(TypeOfAny.special_form) | ||
return None | ||
def _fail_not_attrs_class(ctx: mypy.plugin.FunctionSigContext, t: Type, parent_t: Type) -> None: | ||
@@ -1086,6 +1093,8 @@ def _get_expanded_attr_types( | ||
if init_func is None: | ||
_fail_not_attrs_class(ctx, display_typ, parent_typ) | ||
return None | ||
if isinstance(init_func, AnyType): | ||
return None | ||
init_func = expand_type_by_instance(init_func, typ) | ||
# [1:] to skip the self argument of AttrClass.__init__ | ||
field_names = cast(list[str], init_func.arg_names[1:]) | ||
48 changes: 48 additions & 0 deletionstest-data/unit/check-plugin-attrs.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2496,3 +2496,51 @@ Parent(run_type = None) | ||
c = Child(run_type = None) | ||
reveal_type(c.run_type) # N: Revealed type is "Union[builtins.int, None]" | ||
[builtins fixtures/plugin_attrs.pyi] | ||
[case testAttrsInitOverload1] | ||
# flags: --python-version 3.10 | ||
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) | ||
attrs.evolve(obj, x="2") | ||
uko3211 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
[builtins fixtures/plugin_attrs.pyi] | ||
[case testAttrsInitOverload2] | ||
uko3211 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
# flags: --python-version 3.10 | ||
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) | ||
[builtins fixtures/plugin_attrs.pyi] |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.