diff --git a/gitnexus/bench/python-scope/baseline-fingerprint.txt b/gitnexus/bench/python-scope/baseline-fingerprint.txt index 6a3d76c1c5..ea4d5c6d0e 100644 --- a/gitnexus/bench/python-scope/baseline-fingerprint.txt +++ b/gitnexus/bench/python-scope/baseline-fingerprint.txt @@ -1 +1 @@ -44277912361a8783163b4ca9c5843e4d8f8444116b768881e617f26368bb1cba +87a2b61f39203b4736e9aae28519f95db4926836d186e37e22572c214c85be0b diff --git a/gitnexus/bench/tool-accuracy/known-gaps.json b/gitnexus/bench/tool-accuracy/known-gaps.json index a34df49af6..57975c1b5c 100644 --- a/gitnexus/bench/tool-accuracy/known-gaps.json +++ b/gitnexus/bench/tool-accuracy/known-gaps.json @@ -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": [] } ] } diff --git a/gitnexus/src/core/ingestion/languages/python/query.ts b/gitnexus/src/core/ingestion/languages/python/query.ts index b1892ec20e..c348ecd762 100644 --- a/gitnexus/src/core/ingestion/languages/python/query.ts +++ b/gitnexus/src/core/ingestion/languages/python/query.ts @@ -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) diff --git a/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/app.py b/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/app.py new file mode 100644 index 0000000000..de15c419a5 --- /dev/null +++ b/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/app.py @@ -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() diff --git a/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/models.py b/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/models.py new file mode 100644 index 0000000000..7e570bc1c9 --- /dev/null +++ b/gitnexus/test/fixtures/lang-resolution/python-conditional-return-type/models.py @@ -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() diff --git a/gitnexus/test/fixtures/python-captures-golden/expected-captures.json b/gitnexus/test/fixtures/python-captures-golden/expected-captures.json index 4714aed75f..056da33039 100644 --- a/gitnexus/test/fixtures/python-captures-golden/expected-captures.json +++ b/gitnexus/test/fixtures/python-captures-golden/expected-captures.json @@ -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" diff --git a/gitnexus/test/integration/resolvers/python.test.ts b/gitnexus/test/integration/resolvers/python.test.ts index 402eae3919..08dcc2dea7 100644 --- a/gitnexus/test/integration/resolvers/python.test.ts +++ b/gitnexus/test/integration/resolvers/python.test.ts @@ -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 // ---------------------------------------------------------------------------