@@ -8,12 +8,87 @@ two projects in a monorepo have conflicting definitions (but we want to analyze
88
99In practice these tests cover what we call "desperate module resolution" which, when an import
1010fails, results in us walking up the ancestor directories of the importing file and trying those as
11- "desperate search-paths".
11+ "desperate search-paths" until one works .
1212
1313Currently desperate search-paths are restricted to subdirectories of the first-party search-path
14- (the directory you're running ` ty ` in). Currently we only consider one desperate search-path: the
15- closest ancestor directory containing a ` pyproject.toml ` . In the future we may want to try every
16- ancestor ` pyproject.toml ` or every ancestor directory.
14+ (typically, the directory you're running ` ty ` in).
15+
16+ There are two styles of desperate search-path we consider: "absolute" and "relative". Absolute
17+ desperate search-paths are used for resolving absolute imports (` import a.b.c ` ) while relative
18+ desperate search-paths are used for resolving relative imports (` from .c import x ` ).
19+
20+ Only the closest directory that contains either a ` pyproject.toml ` or ` ty.toml ` is a valid relative
21+ desperate search-path.
22+
23+ All ancestor directories that * do not* contain an ` __init__.py(i) ` are valid absolute desperate
24+ search-paths.
25+
26+ (Distracting detail: to ensure relative desperate search-paths are always valid absolute desperate
27+ search-paths, a directory that contains an ` __init__.py(i) ` * and* either a ` pyproject.toml ` or
28+ ` ty.toml ` is also a valid absolute search-path, but this shouldn't matter in practice, as you do not
29+ typically have those two kinds of file in the same directory.)
30+
31+ ## Relative Desperate Search-Paths
32+
33+ We do not directly resolve relative imports. Instead we have a two-phase process:
34+
35+ 1 . Convert the relative module name ` .c ` to an absolute one ` a.b.c `
36+ 1 . Resolve the absolute import ` a.b.c `
37+
38+ (This allows us to transparently handle packaging semantics that mandate separate directories should
39+ be "logically combined" into a single directory, like namespace packages and stub packages.)
40+
41+ Relative desperate search-paths only appear in step 1, where we compute the module name of the
42+ importing file as the first step in resolving ` . ` to an absolute module name.
43+
44+ In practice, relative desperate search-paths are rarely needed because it usually doesn't matter if
45+ we think ` . ` is ` a.b ` or ` b ` when resolving ` .c ` : the fact that we computed ` a.b ` using our
46+ search-paths means ` a.b.c ` is what will resolve with those search-paths!
47+
48+ There are three caveats to this:
49+
50+ - If the module name we compute is * too short* then too many relative levels will fail to resolve
51+ (` ..c ` resolves in ` a.b ` but not ` b ` ).
52+ - If the module name is * too long* then we may encounter directories that aren't valid module names,
53+ and reject the import (` my-proj.a.b.c ` is not a valid module name).
54+ - Sloppiness will break relative imports in any kind of packaging situation where different
55+ directories are supposed to be "logically combined".
56+
57+ The fact that we restrict desperate resolution to the first-party search-path ("the project you're
58+ working on") allows us to largely dismiss the last concern for the purposes of this discussion. The
59+ remaining two concerns encourage us to find "the longest possible module name without stumbling into
60+ random nonsense directories". When we need relative desperate search-paths we are usually running
61+ into the "too long" problem and "snap to the parent ` pyproject.toml ` (or ` ty.toml ` )" tends to
62+ resolve it well!
63+
64+ As a more aesthetic concern, this approach also ensures that all the files under a given
65+ ` pyproject.toml ` will, when faced with desperation, agree on eachother's relative module names. This
66+ may or may not be important, but it's definitely * reassuring* and * satisfying* !
67+
68+ ## Absolute Desperate Search-Paths
69+
70+ Absolute desperate search-paths are much more load-bearing, because if we're handed the absolute
71+ import ` a.b.c ` then there is only one possible search-path that will properly resolve this the way
72+ the user wants, and if that search-path isn't configured we will fail.
73+
74+ Basic heuristics like checking for ` <working-dir>/src/ ` and resolving editables in the local ` .venv `
75+ work well in most cases, but desperate resolution is needed in a couple key scenarios:
76+
77+ - Test or script directories have a tendency to assume extra search-paths that aren't structurally
78+ obvious ([ notably pytest] ( https://docs.pytest.org/en/stable/explanation/pythonpath.html ) )
79+ - If you open the root of a monorepo in an IDE, you will often have many separate projects but no
80+ configuration explaining this. Absolute imports within each project should resolve things in
81+ that project.
82+
83+ The latter case is often handled reasonably well by the the ` pyproject.toml ` rule that relative
84+ desperate search-paths have. However the more complex testing/scripting scenarios tend to fall over
85+ here -- in the limit pytest will add literally every ancestor to the search-path, and so we simply
86+ need to try every single one and hope * one* works for every absolute import (and it might be a
87+ different one for different imports).
88+
89+ We exclude directories that contain an ` __init__.py(i) ` because there shouldn't be any reasonable
90+ scenario where we need to "truncate" a regular package like that (and pytest's Exciting behaviour
91+ here is explicitly disabled by ` __init__.py ` ).
1792
1893## Invalid Names
1994
@@ -134,13 +209,11 @@ from .mod1 import x
134209
135210# error: [unresolved-import]
136211from . import mod2
137-
138- # error: [unresolved-import]
139212import mod3
140213
141214reveal_type(x) # revealed: Unknown
142215reveal_type(mod2.y) # revealed: Unknown
143- reveal_type(mod3.z) # revealed: Unknown
216+ reveal_type(mod3.z) # revealed: int
144217```
145218
146219` my-proj/tests/mod1.py ` :
@@ -338,21 +411,6 @@ create, and we are now very sensitive to precise search-path ordering.**
338411
339412Here the use of editables means that ` a/ ` has higher priority than ` a/src/a/ ` .
340413
341- Somehow this results in ` a/tests/test1.py ` being able to resolve ` .setup ` but not ` . ` .
342-
343- My best guess is that in this state we can resolve regular modules in ` a/tests/ ` but not namespace
344- packages because we have some extra validation for namespace packages conflicted by regular
345- packages, but that validation isn't applied when we successfully resolve a submodule of the
346- namespace package.
347-
348- In this case, as we find that ` a/tests/test1.py ` matches on the first-party path as ` a.tests.test1 `
349- and is syntactically valid. We then resolve ` a.tests.test1 ` and because the namespace package
350- (` /a/ ` ) comes first we succeed. We then syntactically compute ` . ` to be ` a.tests ` .
351-
352- When we go to lookup ` a.tests.setup ` , whatever grace that allowed ` a.tests.test1 ` to resolve still
353- works so it resolves too. However when we try to resolve ` a.tests ` on its own some additional
354- validation rejects the namespace package conflicting with the regular package.
355-
356414``` toml
357415[environment ]
358416# Setup a venv with editables for a/src/ and b/src/
@@ -385,17 +443,13 @@ b/src/
385443` a/tests/test1.py ` :
386444
387445``` py
388- # TODO : there should be no errors in this file.
389-
390446from .setup import x
391-
392- # error: [unresolved-import]
393447from . import setup
394448from a import y
395449import a
396450
397451reveal_type(x) # revealed: int
398- reveal_type(setup.x) # revealed: Unknown
452+ reveal_type(setup.x) # revealed: int
399453reveal_type(y) # revealed: int
400454reveal_type(a.y) # revealed: int
401455```
@@ -422,17 +476,13 @@ y: int = 10
422476` b/tests/test1.py ` :
423477
424478``` py
425- # TODO : there should be no errors in this file
426-
427479from .setup import x
428-
429- # error: [unresolved-import]
430480from . import setup
431481from b import y
432482import b
433483
434484reveal_type(x) # revealed: str
435- reveal_type(setup.x) # revealed: Unknown
485+ reveal_type(setup.x) # revealed: str
436486reveal_type(y) # revealed: str
437487reveal_type(b.y) # revealed: str
438488```
0 commit comments