-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[mypyc] Transform object.__new__ inside __new__ #19866
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 4 commits
971c56f
a05ea96
8eac14e
94cf8dd
1e241dc
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 |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| ListExpr, | ||
| Lvalue, | ||
| MatchStmt, | ||
| NameExpr, | ||
| OperatorAssignmentStmt, | ||
| RaiseStmt, | ||
| ReturnStmt, | ||
|
|
@@ -170,10 +171,38 @@ def transform_return_stmt(builder: IRBuilder, stmt: ReturnStmt) -> None: | |
| builder.nonlocal_control[-1].gen_return(builder, retval, stmt.line) | ||
|
|
||
|
|
||
| def check_unsupported_cls_assignment(builder: IRBuilder, stmt: AssignmentStmt) -> None: | ||
| fn = builder.fn_info | ||
| method_args = fn.fitem.arg_names | ||
| if fn.name != "__new__" or len(method_args) == 0: | ||
| return | ||
|
|
||
| ir = builder.get_current_class_ir() | ||
| if ir is None or ir.inherits_python or not ir.is_ext_class: | ||
| return | ||
|
|
||
| cls_arg = method_args[0] | ||
| lvalues: list[Expression] = [] | ||
| for lvalue in stmt.lvalues: | ||
| if isinstance(lvalue, (TupleExpr, ListExpr)): | ||
| lvalues += lvalue.items | ||
| else: | ||
| lvalues.append(lvalue) | ||
|
||
|
|
||
| for lvalue in lvalues: | ||
| if isinstance(lvalue, NameExpr) and lvalue.name == cls_arg: | ||
| # Disallowed because it could break the transformation of object.__new__ calls | ||
| # inside __new__ methods. | ||
| builder.error( | ||
| f"Assignment to argument {cls_arg} in __new__ method unsupported", stmt.line | ||
|
||
| ) | ||
|
|
||
|
|
||
| def transform_assignment_stmt(builder: IRBuilder, stmt: AssignmentStmt) -> None: | ||
| lvalues = stmt.lvalues | ||
| assert lvalues | ||
| builder.disallow_class_assignments(lvalues, stmt.line) | ||
| check_unsupported_cls_assignment(builder, stmt) | ||
| first_lvalue = lvalues[0] | ||
| if stmt.type and isinstance(stmt.rvalue, TempNode): | ||
| # This is actually a variable annotation without initializer. Don't generate | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.