-
-
Notifications
You must be signed in to change notification settings - Fork 292
Ensure the *_fields
attributes of __init__
/postinit
are cohesive
#1218
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
Changes from all commits
92580dc
89a5a98
5c91e84
21d3496
ac264e4
1eb489c
445220c
720b7d8
1bcdb30
607cc43
3dd7c72
1fbfe2b
481805c
b5af638
db0ea1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -752,6 +752,8 @@ def __init__( | |
vararg: Optional[str] = None, | ||
kwarg: Optional[str] = None, | ||
parent: Optional[NodeNG] = None, | ||
lineno: Optional[int] = None, | ||
col_offset: Optional[int] = None, | ||
) -> None: | ||
""" | ||
:param vararg: The name of the variable length arguments. | ||
|
@@ -760,7 +762,7 @@ def __init__( | |
|
||
:param parent: The parent node in the syntax tree. | ||
""" | ||
super().__init__(parent=parent) | ||
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) | ||
|
||
self.vararg: Optional[str] = vararg # can be None | ||
"""The name of the variable length arguments.""" | ||
|
@@ -1284,6 +1286,7 @@ def __init__( | |
lineno: Optional[int] = None, | ||
col_offset: Optional[int] = None, | ||
parent: Optional[NodeNG] = None, | ||
simple: Optional[int] = None, | ||
) -> None: | ||
""" | ||
:param lineno: The line that this node appears on in the source code. | ||
|
@@ -1302,16 +1305,17 @@ def __init__( | |
self.value: Optional[NodeNG] = None # can be None | ||
"""The value being assigned to the variables.""" | ||
|
||
self.simple: Optional[int] = None | ||
self.simple: Optional[int] = simple | ||
"""Whether :attr:`target` is a pure name or a complex statement.""" | ||
|
||
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) | ||
|
||
@decorators.deprecate_arguments("simple", hint="Pass it to __init__ instead") | ||
def postinit( | ||
self, | ||
target: NodeNG, | ||
annotation: NodeNG, | ||
simple: int, | ||
simple: Optional[int] = None, | ||
value: Optional[NodeNG] = None, | ||
) -> None: | ||
"""Do some setup after initialisation. | ||
|
@@ -1328,7 +1332,7 @@ def postinit( | |
self.target = target | ||
self.annotation = annotation | ||
self.value = value | ||
self.simple = simple | ||
self.simple = simple or self.simple | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can easily break. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this was a forehead-slapper. |
||
|
||
def get_children(self): | ||
yield self.target | ||
|
@@ -1759,7 +1763,13 @@ class Comprehension(NodeNG): | |
optional_assign = True | ||
"""Whether this node optionally assigns a variable.""" | ||
|
||
def __init__(self, parent: Optional[NodeNG] = None) -> None: | ||
def __init__( | ||
self, | ||
parent: Optional[NodeNG] = None, | ||
lineno: Optional[int] = None, | ||
col_offset: Optional[int] = None, | ||
Comment on lines
+1769
to
+1770
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, the ast |
||
is_async: Optional[bool] = None, | ||
) -> None: | ||
""" | ||
:param parent: The parent node in the syntax tree. | ||
""" | ||
|
@@ -1772,18 +1782,19 @@ def __init__(self, parent: Optional[NodeNG] = None) -> None: | |
self.ifs: typing.List[NodeNG] = [] | ||
"""The contents of any if statements that filter the comprehension.""" | ||
|
||
self.is_async: Optional[bool] = None | ||
self.is_async: Optional[bool] = is_async | ||
"""Whether this is an asynchronous comprehension or not.""" | ||
|
||
super().__init__(parent=parent) | ||
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) | ||
|
||
# pylint: disable=redefined-builtin; same name as builtin ast module. | ||
@decorators.deprecate_arguments("is_async", hint="Pass to __init__ instead") | ||
def postinit( | ||
self, | ||
target: Optional[NodeNG] = None, | ||
iter: Optional[NodeNG] = None, | ||
ifs: Optional[typing.List[NodeNG]] = None, | ||
is_async: Optional[bool] = None, | ||
is_async: Optional[bool] = None, # @TODO: Deprecate and remove | ||
) -> None: | ||
"""Do some setup after initialisation. | ||
|
||
|
@@ -1800,7 +1811,7 @@ def postinit( | |
self.iter = iter | ||
if ifs is not None: | ||
self.ifs = ifs | ||
self.is_async = is_async | ||
self.is_async = is_async or self.is_async | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can break too! |
||
|
||
def assign_type(self): | ||
"""The type of assignment that this node performs. | ||
|
@@ -1846,7 +1857,7 @@ class Const(mixins.NoChildrenMixin, NodeNG, Instance): | |
<Const.bytes l.1 at 0x7f23b2e35a20>] | ||
""" | ||
|
||
_other_fields = ("value",) | ||
_other_fields = ("value", "kind") | ||
|
||
def __init__( | ||
self, | ||
|
@@ -2630,7 +2641,7 @@ class ImportFrom(mixins.NoChildrenMixin, mixins.ImportFromMixin, Statement): | |
<ImportFrom l.1 at 0x7f23b2e415c0> | ||
""" | ||
|
||
_other_fields = ("modname", "names", "level") | ||
_other_fields = ("fromname", "names", "level") | ||
|
||
def __init__( | ||
self, | ||
|
@@ -2655,7 +2666,8 @@ def __init__( | |
|
||
:param parent: The parent node in the syntax tree. | ||
""" | ||
self.modname: Optional[str] = fromname # can be None | ||
self.fromname: Optional[str] = fromname # can be None | ||
self.modname = self.fromname # For backwards compatibility | ||
Comment on lines
+2669
to
+2670
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be part of this PR. IMO it's not worth it to do this change in isolation, there is no really need for it. |
||
"""The module that is being imported from. | ||
|
||
This is ``None`` for relative imports. | ||
|
@@ -4076,6 +4088,7 @@ class FormattedValue(NodeNG): | |
""" | ||
|
||
_astroid_fields = ("value", "format_spec") | ||
_other_other_fields = ("conversion",) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should this be in |
||
|
||
def __init__( | ||
self, | ||
|
@@ -4252,7 +4265,7 @@ class EvaluatedObject(NodeNG): | |
|
||
name = "EvaluatedObject" | ||
_astroid_fields = ("original",) | ||
_other_fields = ("value",) | ||
_other_other_fields = ("value",) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why here? |
||
|
||
def __init__( | ||
self, original: NodeNG, value: typing.Union[NodeNG, util.Uninferable] | ||
|
@@ -4333,11 +4346,17 @@ class MatchCase(mixins.MultiLineBlockMixin, NodeNG): | |
_astroid_fields = ("pattern", "guard", "body") | ||
_multi_line_block_fields = ("body",) | ||
|
||
def __init__(self, *, parent: Optional[NodeNG] = None) -> None: | ||
def __init__( | ||
self, | ||
*, | ||
lineno: Optional[int] = None, | ||
col_offset: Optional[int] = None, | ||
Comment on lines
+4352
to
+4353
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
parent: Optional[NodeNG] = None, | ||
) -> None: | ||
self.pattern: Pattern | ||
self.guard: Optional[NodeNG] | ||
self.body: typing.List[NodeNG] | ||
super().__init__(parent=parent) | ||
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) | ||
|
||
def postinit( | ||
self, | ||
|
@@ -4519,24 +4538,27 @@ def __init__( | |
lineno: Optional[int] = None, | ||
col_offset: Optional[int] = None, | ||
parent: Optional[NodeNG] = None, | ||
*, | ||
kwd_attrs: typing.Optional[typing.List[str]] = None, | ||
) -> None: | ||
self.cls: NodeNG | ||
self.patterns: typing.List[Pattern] | ||
self.kwd_attrs: typing.List[str] | ||
self.kwd_attrs = kwd_attrs or [] | ||
self.kwd_patterns: typing.List[Pattern] | ||
super().__init__(lineno=lineno, col_offset=col_offset, parent=parent) | ||
|
||
@decorators.deprecate_arguments("kwd_attrs", hint="Pass it to __init__ instead") | ||
def postinit( | ||
self, | ||
*, | ||
cls: NodeNG, | ||
patterns: typing.List[Pattern], | ||
kwd_attrs: typing.List[str], | ||
kwd_patterns: typing.List[Pattern], | ||
kwd_attrs: typing.Optional[typing.List[str]] = None, | ||
Comment on lines
-4534
to
+4557
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO it does make more sense to keep |
||
) -> None: | ||
self.cls = cls | ||
self.patterns = patterns | ||
self.kwd_attrs = kwd_attrs | ||
self.kwd_attrs = kwd_attrs or self.kwd_attrs or [] | ||
self.kwd_patterns = kwd_patterns | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -474,6 +474,7 @@ def __init__( | |
package=None, | ||
parent=None, | ||
pure_python=True, | ||
future_imports=None, | ||
): | ||
""" | ||
:param name: The name of the module. | ||
|
@@ -496,6 +497,9 @@ def __init__( | |
|
||
:param pure_python: Whether the ast was built from source. | ||
:type pure_python: bool or None | ||
|
||
:param future_imports: The imports from ``__future__``. | ||
:type future_imports: Optional[Set[str]] | ||
""" | ||
self.name = name | ||
self.doc = doc | ||
|
@@ -514,7 +518,7 @@ def __init__( | |
|
||
:type: list(NodeNG) or None | ||
""" | ||
self.future_imports = set() | ||
self.future_imports = future_imports or set() | ||
|
||
# pylint: enable=redefined-builtin | ||
|
||
|
@@ -1393,7 +1397,6 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): | |
_other_fields = ("name", "doc") | ||
_other_other_fields = ( | ||
"locals", | ||
"_type", | ||
"type_comment_returns", | ||
"type_comment_args", | ||
) | ||
|
@@ -2000,7 +2003,7 @@ def my_meth(self, arg): | |
), | ||
) | ||
_other_fields = ("name", "doc") | ||
_other_other_fields = ("locals", "_newstyle") | ||
_other_other_fields = ("locals", "_newstyle", "_metaclass") | ||
_newstyle = None | ||
|
||
def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None): | ||
|
@@ -2105,8 +2108,7 @@ def postinit( | |
:param keywords: The keywords given to the class definition. | ||
:type keywords: list(Keyword) or None | ||
""" | ||
if keywords is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a very strange way to do it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is strange about it? The default for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well usually you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See the relevant PR for this change here: |
||
self.keywords = keywords | ||
self.keywords = keywords | ||
self.bases = bases | ||
self.body = body | ||
self.decorators = decorators | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ast node
arguments
doesn't havelineno
orcol_offset
information. IMO it doesn't make sense to add them. https://docs.python.org/3.10/library/ast.html