Skip to content

Mandatory fields for Assign #2061

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

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Release date: TBA
Closes #1780

* Improved signature of the ``__init__`` and ``__postinit__`` methods of the following nodes:
- ``nodes.Assign``
- ``nodes.AssignName``
- ``nodes.BinOp``
- ``nodes.Delete``
Expand Down
61 changes: 12 additions & 49 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1170,62 +1170,25 @@ class Assign(_base_nodes.AssignTypeNode, _base_nodes.Statement):
<Assign l.1 at 0x7effe1db8550>
"""

_astroid_fields = ("targets", "value")
_other_other_fields = ("type_annotation",)

def __init__(
self,
lineno: int | None = None,
col_offset: int | None = None,
parent: NodeNG | None = None,
*,
end_lineno: int | None = None,
end_col_offset: int | None = None,
) -> None:
"""
:param lineno: The line that this node appears on in the source code.

:param col_offset: The column that this node appears on in the
source code.

:param parent: The parent node in the syntax tree.

:param end_lineno: The last line this node appears on in the source code.

:param end_col_offset: The end column this node appears on in the
source code. Note: This is after the last symbol.
"""
self.targets: list[NodeNG] = []
"""What is being assigned to."""
targets: list[NodeNG]
"""What is being assigned to."""

self.value: NodeNG | None = None
"""The value being assigned to the variables."""
value: NodeNG
"""The value being assigned to the variables."""

self.type_annotation: NodeNG | None = None # can be None
"""If present, this will contain the type annotation passed by a type comment"""
type_annotation: NodeNG | None
"""If present, this will contain the type annotation passed by a type comment"""

super().__init__(
lineno=lineno,
col_offset=col_offset,
end_lineno=end_lineno,
end_col_offset=end_col_offset,
parent=parent,
)
_astroid_fields = ("targets", "value")
_other_other_fields = ("type_annotation",)

def postinit(
Copy link
Collaborator

Choose a reason for hiding this comment

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

newnode.postinit(

Could you add asserts here that the typing we add here is correct?

And could you add a changelog entry? For the rest this LGTM!

self,
targets: list[NodeNG] | None = None,
value: NodeNG | None = None,
type_annotation: NodeNG | None = None,
targets: list[NodeNG],
value: NodeNG,
type_annotation: NodeNG | None,
) -> None:
"""Do some setup after initialisation.

:param targets: What is being assigned to.
:param value: The value being assigned to the variables.
:param type_annotation:
"""
if targets is not None:
self.targets = targets
self.targets = targets
self.value = value
self.type_annotation = type_annotation

Expand Down