fix(python): retain nullable factory receiver types (#3595)

* fix(python): retain nullable factory receiver types

* test(accuracy): remove repaired Python gap allowances

* test(bench): refresh Python capture fingerprint

* fix(python): type `None or call()` assignments like `call() or None`

`None or make_user()` always evaluates to the call, but only the
`call() or None` order matched, so the variable lost the factory's
return type. Add the mirrored boolean pattern, matching the two
conditional-expression orders the PR already supports.

Fixture gains `none_or` (resolves to User.save) and `none_and` (stays
unresolved: `None and x` is always None). The capture golden changes only
for the edited fixture, and the python-scope baseline is re-recorded
(`measure.mjs --check` passes, scaling 1.021).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
azizur100389 2026-10-18 15:39:40 +01:00 • committed by GitHub
parent 39e0ced68d
commit 702c917377
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 149 additions and 13 deletions

View file

@ -1 +1 @@
44277912361a8783163b4ca9c5843e4d8f8444116b768881e617f26368bb1cba
87a2b61f39203b4736e9aae28519f95db4926836d186e37e22572c214c85be0b

View file

@ -32,23 +32,11 @@
"source handleUnsafe@13"
]
},
{
"id": "python.factory-conditional",
"issue": 3498,
"reason": "Conditional RHS loses annotated factory return type (#3498).",
"baselineActual": []
},
{
"id": "python.factory-qualified",
"issue": 3498,
"reason": "Module-qualified factory return type is not followed (#3498).",
"baselineActual": []
},
{
"id": "python.factory-boolean",
"issue": 3498,
"reason": "Boolean RHS loses annotated factory return type (#3498).",
"baselineActual": []
}
]
}

View file

@ -103,6 +103,38 @@ const PYTHON_SCOPE_QUERY = `
left: (identifier) @type-binding.name
right: (identifier) @type-binding.type) @type-binding.alias
;; A nullable alternative preserves the non-None arm's inferred type. Keep
;; these patterns limited to a literal None arm: two value-producing arms do
;; not establish one receiver type.
(assignment
left: (identifier) @type-binding.name @call-result-assignment.lhs
right: (conditional_expression
(call function: (identifier) @type-binding.type) @call-result-assignment.call
(_)
(none))) @type-binding.constructor
(assignment
left: (identifier) @type-binding.name @call-result-assignment.lhs
right: (conditional_expression
(none)
(_)
(call function: (identifier) @type-binding.type) @call-result-assignment.call)) @type-binding.constructor
(assignment
left: (identifier) @type-binding.name @call-result-assignment.lhs
right: (boolean_operator
left: (call function: (identifier) @type-binding.type) @call-result-assignment.call
operator: "or"
right: (none))) @type-binding.constructor
;; \`None or call()\` always evaluates to the call.
(assignment
left: (identifier) @type-binding.name @call-result-assignment.lhs
right: (boolean_operator
left: (none)
operator: "or"
right: (call function: (identifier) @type-binding.type) @call-result-assignment.call)) @type-binding.constructor
;; For-loop iterable of an already-typed variable:
;; def f(users: list[User]):
;; for u in users: # u: users (chained via post-pass)

View file

@ -0,0 +1,56 @@
from models import User, make_other, make_user
def direct():
user = make_user()
user.save()
def conditional(flag):
user = make_user() if flag else None
if user is not None:
user.save()
def reverse_conditional(flag):
user = None if flag else make_user()
if user is not None:
user.save()
def or_none():
user = make_user() or None
if user is not None:
user.save()
def none_or():
user = None or make_user()
if user is not None:
user.save()
def class_constructor(flag):
user = User() if flag else None
if user is not None:
user.save()
def two_factories(flag):
user = make_user() if flag else make_other()
user.save()
def and_none():
user = make_user() and None
user.save()
def or_other():
user = make_user() or make_other()
user.save()
def none_and():
user = None and make_user()
user.save()

View file

@ -0,0 +1,16 @@
class User:
def save(self):
return True
class Other:
def save(self):
return False
def make_user() -> User:
return User()
def make_other() -> Other:
return Other()

View file

@ -191,6 +191,14 @@
"captureGroups": 4,
"digest": "d6ead25daf8f609f1ea2ce79ccf1a88ad3c0ee2d0c9ced0ab13e8f1e729eecb5"
},
"python-conditional-return-type/app.py": {
"captureGroups": 67,
"digest": "5d3b46147452df4adaa3a7be19ba7b445670ee1ce10bd01cacc2c7fd5efbba4f"
},
"python-conditional-return-type/models.py": {
"captureGroups": 21,
"digest": "f25ac8772dc1a4154e8d4bf8e599bd999e26458267c593e9d46c456ab2222c9e"
},
"python-constructor-calls/app.py": {
"captureGroups": 8,
"digest": "877f75415654e57abd197eff88086011d6ac4d1af5b70b0113d0ae1ae2daf077"

View file

@ -2501,6 +2501,42 @@ describe('Python return type inference', () => {
});
});
describe('Python nullable assignment result types (#3498)', () => {
let result: PipelineResult;
beforeAll(async () => {
result = await runPipelineFromRepo(
path.join(FIXTURES, 'python-conditional-return-type'),
() => {},
);
}, 60000);
it.each([
'direct',
'conditional',
'reverse_conditional',
'or_none',
'none_or',
'class_constructor',
])('resolves %s to User.save', (source) => {
const saveCalls = getRelationships(result, 'CALLS').filter(
(edge) => edge.source === source && edge.target === 'save',
);
expect(saveCalls).toHaveLength(1);
expect(saveCalls[0]?.rel.targetId).toContain('User.save');
});
it.each(['two_factories', 'and_none', 'none_and', 'or_other'])(
'does not infer a single receiver type for %s',
(source) => {
const saveCalls = getRelationships(result, 'CALLS').filter(
(edge) => edge.source === source && edge.target === 'save',
);
expect(saveCalls).toHaveLength(0);
},
);
});
// ---------------------------------------------------------------------------
// Issue #289: static/classmethod classes must have HAS_METHOD edges
// ---------------------------------------------------------------------------