Repository navigation
fix(resolve): type Python locals built through a module and resolve calls through their fields #458
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| /** | ||
| * A Python local typed by a constructor named through a module | ||
| * (`bp = pkg.Blueprint()`, `c.Blueprint()`, `core.Blueprint()`) is typed like | ||
| * one built from a class imported by name, and `local.field.method()` resolves | ||
| * on the class the field holds, as `self.field.method()` does. | ||
| */ | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import * as fs from 'node:fs'; | ||
| import * as os from 'node:os'; | ||
| import * as path from 'node:path'; | ||
| import { CodeGraph } from '../src'; | ||
|
|
||
| let dir: string | undefined; | ||
| let graph: CodeGraph | undefined; | ||
| afterEach(() => { | ||
| graph?.close(); | ||
| graph = undefined; | ||
| if (dir) fs.rmSync(dir, { recursive: true, force: true }); | ||
| dir = undefined; | ||
| }); | ||
|
|
||
| async function project(files: Record<string, string>) { | ||
| dir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-py-module-receiver-')); | ||
| for (const [name, source] of Object.entries(files)) { | ||
| fs.mkdirSync(path.dirname(path.join(dir, name)), { recursive: true }); | ||
| fs.writeFileSync(path.join(dir, name), source); | ||
| } | ||
| graph = await CodeGraph.init(dir, { index: true }); | ||
| graph.resolveReferences(); | ||
| } | ||
|
|
||
| /** `Qualified::name` of every call target of the function `caller`. */ | ||
| function calls(caller: string): string[] { | ||
| const from = graph!.getNodesByKind('function').find(n => n.name === caller); | ||
| expect(from, caller).toBeDefined(); | ||
| return graph! | ||
| .getOutgoingEdges(from!.id) | ||
| .filter(e => e.kind === 'calls') | ||
| .map(e => graph!.getNode(e.target)!.qualifiedName) | ||
| .sort(); | ||
| } | ||
|
|
||
| const core = `class Config(dict): | ||
| def from_mapping(self, m): | ||
| return True | ||
|
|
||
|
|
||
| class Base: | ||
| def add_x(self): | ||
| return 1 | ||
|
|
||
|
|
||
| class Blueprint(Base): | ||
| def own(self): | ||
| return 2 | ||
|
|
||
|
|
||
| class Flask: | ||
| def __init__(self): | ||
| self.config = Config() | ||
|
|
||
| def test_client(self): | ||
| return 3 | ||
| `; | ||
| const files = (test: string, extra: Record<string, string> = {}) => ({ | ||
| 'pkg/__init__.py': 'from .core import Blueprint as Blueprint\nfrom .core import Flask as Flask\n', | ||
| 'pkg/core.py': core, | ||
| 'tests/test_a.py': test, | ||
| ...extra, | ||
| }); | ||
|
|
||
| describe('a local typed by a module-qualified constructor', () => { | ||
| it.each([ | ||
| ['a re-exported class', 'import pkg\n\n\ndef t():\n bp = pkg.Blueprint()\n bp.own()\n'], | ||
| ['a dotted module path', 'import pkg.core\n\n\ndef t():\n bp = pkg.core.Blueprint()\n bp.own()\n'], | ||
| ['an aliased module', 'import pkg.core as c\n\n\ndef t():\n bp = c.Blueprint()\n bp.own()\n'], | ||
| ['a module imported by name', 'from pkg import core\n\n\ndef t():\n bp = core.Blueprint()\n bp.own()\n'], | ||
| ])('reaches the class method through %s', async (_label, test) => { | ||
| await project(files(test)); | ||
| expect(calls('t')).toEqual(['Blueprint::own']); | ||
| }); | ||
|
|
||
| it('reaches an inherited method', async () => { | ||
| await project(files('import pkg\n\n\ndef t():\n bp = pkg.Blueprint()\n bp.add_x()\n')); | ||
| expect(calls('t')).toEqual(['Base::add_x']); | ||
| }); | ||
|
|
||
| it('does not type a local from a call on another local object', async () => { | ||
| await project( | ||
| files( | ||
| 'import pkg\n\n\ndef make(factory):\n bp = factory.Blueprint()\n bp.own()\n', | ||
| { 'other.py': 'class Blueprint:\n def own(self):\n return 9\n' }, | ||
| ), | ||
| ); | ||
| expect(calls('make')).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('a parameter that shadows the imported module', () => { | ||
| it('does not type a local from the shadowed module', async () => { | ||
| await project( | ||
| files('import pkg\n\n\ndef t(pkg):\n bp = pkg.Blueprint()\n bp.own()\n'), | ||
| ); | ||
| expect(calls('t')).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not follow a field of a local built from the shadowed module', async () => { | ||
| await project( | ||
| files('import pkg\n\n\ndef t(pkg):\n app = pkg.Flask()\n app.config.from_mapping({})\n'), | ||
| ); | ||
| expect(calls('t')).toEqual([]); | ||
| }); | ||
| }); | ||
|
|
||
| describe('a call through a field of a typed local', () => { | ||
| it.each([ | ||
| ['a class imported by name', 'from pkg import Flask\n\n\ndef t():\n app = Flask()\n app.config.from_mapping({})\n'], | ||
| ['a module-qualified class', 'import pkg\n\n\ndef t():\n app = pkg.Flask()\n app.config.from_mapping({})\n'], | ||
| ])('resolves on the class the field holds for %s', async (_label, test) => { | ||
| await project(files(test)); | ||
| expect(calls('t')).toEqual(['Config::from_mapping']); | ||
| }); | ||
|
|
||
| it('does not borrow a same-named method of an unrelated class', async () => { | ||
| await project( | ||
| files('from pkg import Flask\n\n\ndef t():\n app = Flask()\n app.config.save({})\n', { | ||
| 'other.py': 'class Unrelated:\n def save(self, m):\n return 1\n', | ||
| }), | ||
| ); | ||
| expect(calls('t')).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not resolve when the local has no known type', async () => { | ||
| await project(files('def t(app):\n app.config.from_mapping({})\n')); | ||
| expect(calls('t')).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not resolve a field the class never assigns', async () => { | ||
| await project(files('from pkg import Flask\n\n\ndef t():\n app = Flask()\n app.missing.from_mapping({})\n')); | ||
| expect(calls('t')).toEqual([]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.