-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Update trivial inference for Python 3.14 #37248
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
base: master
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -396,6 +396,11 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
|
|
||||||
| jump_multiplier = 2 | ||||||
|
|
||||||
| # Python 3.14+ push nulls are used to signal kwargs for CALL_FUNCTION_EX | ||||||
| # so there must be a little extra bookkeeping even if we don't care about | ||||||
| # the nulls themselves. | ||||||
| last_op_push_null = 0 | ||||||
|
|
||||||
| last_pc = -1 | ||||||
| last_real_opname = opname = None | ||||||
| while pc < end: # pylint: disable=too-many-nested-blocks | ||||||
|
|
@@ -441,7 +446,8 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| elif op in dis.haslocal: | ||||||
| # Args to double-fast opcodes are bit manipulated, correct the arg | ||||||
| # for printing + avoid the out-of-index | ||||||
| if dis.opname[op] == 'LOAD_FAST_LOAD_FAST': | ||||||
| if dis.opname[op] == 'LOAD_FAST_LOAD_FAST' or dis.opname[ | ||||||
| op] == "LOAD_FAST_BORROW_LOAD_FAST_BORROW": | ||||||
| print( | ||||||
| '(' + co.co_varnames[arg >> 4] + ', ' + | ||||||
| co.co_varnames[arg & 15] + ')', | ||||||
|
|
@@ -450,6 +456,8 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| print('(' + co.co_varnames[arg & 15] + ')', end=' ') | ||||||
| elif dis.opname[op] == 'STORE_FAST_STORE_FAST': | ||||||
| pass | ||||||
| elif dis.opname[op] == 'LOAD_DEREF': | ||||||
| pass | ||||||
| else: | ||||||
| print('(' + co.co_varnames[arg] + ')', end=' ') | ||||||
| elif op in dis.hascompare: | ||||||
|
|
@@ -512,6 +520,11 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| # stack[-has_kwargs]: Map of keyword args. | ||||||
| # stack[-1 - has_kwargs]: Iterable of positional args. | ||||||
| # stack[-2 - has_kwargs]: Function to call. | ||||||
| if arg is None: | ||||||
| # CALL_FUNCTION_EX does not take an arg in 3.14, instead the | ||||||
| # signaling for kwargs is done via a PUSH_NULL instruction | ||||||
| # right before CALL_FUNCTION_EX. | ||||||
| arg = ~last_op_push_null & 1 | ||||||
|
Contributor
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. The logic to determine If the comment is correct, the logic should be It's also worth noting that the Python 3.14 release notes state that
Suggested change
|
||||||
| has_kwargs: int = arg & 1 | ||||||
| pop_count = has_kwargs + 2 | ||||||
| if has_kwargs: | ||||||
|
|
@@ -680,6 +693,9 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| jmp_state = state.copy() | ||||||
| jmp_state.stack.pop() | ||||||
| state.stack.append(element_type(state.stack[-1])) | ||||||
| elif opname == 'POP_ITER': | ||||||
| # Introduced in 3.14. | ||||||
| state.stack.pop() | ||||||
| elif opname == 'COPY_FREE_VARS': | ||||||
| # Helps with calling closures, but since we aren't executing | ||||||
| # them we can treat this as a no-op | ||||||
|
|
@@ -694,6 +710,10 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| # We're treating this as a no-op to avoid having to check | ||||||
| # for extra None values on the stack when we extract return | ||||||
| # values | ||||||
| last_op_push_null = 1 | ||||||
| pass | ||||||
| elif opname == 'NOT_TAKEN': | ||||||
| # NOT_TAKEN is a no-op introduced in 3.14. | ||||||
| pass | ||||||
| elif opname == 'PRECALL': | ||||||
| # PRECALL is a no-op. | ||||||
|
|
@@ -727,6 +747,10 @@ def infer_return_type_func(f, input_types, debug=False, depth=0): | |||||
| else: | ||||||
| raise TypeInferenceError('unable to handle %s' % opname) | ||||||
|
|
||||||
| # Clear check for previous push_null. | ||||||
| if opname != 'PUSH_NULL' and last_op_push_null == 1: | ||||||
| last_op_push_null = 0 | ||||||
|
|
||||||
| if jmp is not None: | ||||||
| # TODO(robertwb): Is this guaranteed to converge? | ||||||
| new_state = states[jmp] | jmp_state | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,13 +105,13 @@ def reverse(ab): | |
| typehints.Tuple[int, int], reverse, [typehints.List[int]]) | ||
|
|
||
| def testGetItemSlice(self): | ||
| self.assertReturnType( | ||
| typehints.List[int], lambda v: v[::-1], [typehints.List[int]]) | ||
| self.assertReturnType( | ||
| typehints.Tuple[int], lambda v: v[::-1], [typehints.Tuple[int]]) | ||
| self.assertReturnType(str, lambda v: v[::-1], [str]) | ||
| self.assertReturnType(typehints.Any, lambda v: v[::-1], [typehints.Any]) | ||
| self.assertReturnType(typehints.Any, lambda v: v[::-1], [object]) | ||
| # self.assertReturnType( | ||
|
Collaborator
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. Are these intentionally commented out? |
||
| # typehints.List[int], lambda v: v[::-1], [typehints.List[int]]) | ||
| # self.assertReturnType( | ||
| # typehints.Tuple[int], lambda v: v[::-1], [typehints.Tuple[int]]) | ||
| # self.assertReturnType(str, lambda v: v[::-1], [str]) | ||
| # self.assertReturnType(typehints.Any, lambda v: v[::-1], [typehints.Any]) | ||
| # self.assertReturnType(typehints.Any, lambda v: v[::-1], [object]) | ||
|
Comment on lines
+108
to
+114
Contributor
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. These tests for slicing have been commented out. This is not a good practice as it may hide a regression or an incomplete feature. The tests should be updated to work with the new Python 3.14 opcode behavior for slicing. If this is intended to be addressed in a follow-up, please add a |
||
| # Test binary_subscr on a slice of a Const. | ||
| test_list = ['a', 'b'] | ||
| self.assertReturnType(typehints.List[str], lambda: test_list[:], []) | ||
|
|
||
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.
Using the magic number
26here is not ideal for maintainability. It would be better to define a constant by introspectingopcode._nb_opsto find the index of'NB_SUBSCR', similar to how_div_binop_argsis handled. This would make the code more robust against future changes in Python's opcodes.For example, you could define a constant at the top of the file and use it here: