Skip to content

Commit d9f6fa2

Browse files
committed
relabel
1 parent 9662ee1 commit d9f6fa2

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

crates/ty_python_semantic/resources/mdtest/import/nonstandard_conventions.md

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ This file currently covers the following details:
2626
(equivalent to `from . import a as a`). This is required to properly handle many stubs in the
2727
wild. Currently it must be *exactly* `from . import ...`.
2828

29+
Note: almost all tests in here have a stub and non-stub version, because we're interested in both
30+
defining symbols *at all* and re-exporting them.
31+
2932
## Relative `from` Import of Direct Submodule in `__init__`
3033

3134
We consider the `from . import submodule` idiom in an `__init__.pyi` an explicit re-export.
3235

36+
### In Stub
37+
3338
`mypackage/__init__.pyi`:
3439

3540
```pyi
@@ -58,7 +63,7 @@ reveal_type(mypackage.imported.X) # revealed: int
5863
reveal_type(mypackage.fails.Y) # revealed: Unknown
5964
```
6065

61-
## Relative `from` Import of Direct Submodule in `__init__` (Non-Stub Check)
66+
### In Non-Stub
6267

6368
`mypackage/__init__.py`:
6469

@@ -94,6 +99,8 @@ If an absolute `from...import` happens to import a submodule (i.e. it's equivale
9499
`from . import y`) we do not treat it as a re-export. We could, but we don't. (This is an arbitrary
95100
decision and can be changed!)
96101

102+
### In Stub
103+
97104
`mypackage/__init__.pyi`:
98105

99106
```pyi
@@ -124,7 +131,7 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
124131
reveal_type(mypackage.fails.Y) # revealed: Unknown
125132
```
126133

127-
## Absolute `from` Import of Direct Submodule in `__init__` (Non-Stub Check)
134+
### In Non-Stub
128135

129136
`mypackage/__init__.py`:
130137

@@ -159,6 +166,8 @@ reveal_type(mypackage.fails.Y) # revealed: Unknown
159166
An `import` that happens to import a submodule does not expose the submodule as an attribute. (This
160167
is an arbitrary decision and can be changed!)
161168

169+
### In Stub
170+
162171
`mypackage/__init__.pyi`:
163172

164173
```pyi
@@ -181,7 +190,7 @@ import mypackage
181190
reveal_type(mypackage.imported.X) # revealed: Unknown
182191
```
183192

184-
## Import of Direct Submodule in `__init__` (Non-Stub Check)
193+
### In Non-Stub
185194

186195
`mypackage/__init__.py`:
187196

@@ -210,6 +219,8 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
210219
`from .submodule import nested` in an `__init__.pyi` does not re-export `mypackage.submodule`,
211220
`mypackage.submodule.nested`, or `nested`.
212221

222+
### In Stub
223+
213224
`mypackage/__init__.pyi`:
214225

215226
```pyi
@@ -244,7 +255,7 @@ reveal_type(mypackage.nested) # revealed: Unknown
244255
reveal_type(mypackage.nested.X) # revealed: Unknown
245256
```
246257

247-
## Relative `from` Import of Nested Submodule in `__init__` (Non-Stub Check)
258+
### In Non-Stub
248259

249260
`from .submodule import nested` in an `__init__.py` exposes `mypackage.submodule` and `nested`.
250261

@@ -285,6 +296,8 @@ reveal_type(mypackage.nested.X) # revealed: int
285296
`from mypackage.submodule import nested` in an `__init__.pyi` does not re-export
286297
`mypackage.submodule`, `mypackage.submodule.nested`, or `nested`.
287298

299+
### In Stub
300+
288301
`mypackage/__init__.pyi`:
289302

290303
```pyi
@@ -320,7 +333,7 @@ reveal_type(mypackage.nested) # revealed: Unknown
320333
reveal_type(mypackage.nested.X) # revealed: Unknown
321334
```
322335

323-
## Absolute `from` Import of Nested Submodule in `__init__` (Non-Stub Check)
336+
### In Non-Stub
324337

325338
`from mypackage.submodule import nested` in an `__init__.py` only creates `nested`.
326339

@@ -362,6 +375,8 @@ reveal_type(mypackage.nested.X) # revealed: int
362375
`import mypackage.submodule.nested` in an `__init__.pyi` does not re-export `mypackage.submodule` or
363376
`mypackage.submodule.nested`.
364377

378+
### In Stub
379+
365380
`mypackage/__init__.pyi`:
366381

367382
```pyi
@@ -392,7 +407,7 @@ reveal_type(mypackage.submodule.nested) # revealed: Unknown
392407
reveal_type(mypackage.submodule.nested.X) # revealed: Unknown
393408
```
394409

395-
## Import of Nested Submodule in `__init__` (Non-Stub Check)
410+
### In Non-Stub
396411

397412
`import mypackage.submodule.nested` in an `__init__.py` does not define `mypackage.submodule` or
398413
`mypackage.submodule.nested` outside the package.
@@ -432,6 +447,8 @@ reveal_type(mypackage.submodule.nested.X) # revealed: Unknown
432447

433448
Renaming the submodule to something else disables the `__init__.pyi` idiom.
434449

450+
### In Stub
451+
435452
`mypackage/__init__.pyi`:
436453

437454
```pyi
@@ -455,7 +472,7 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
455472
reveal_type(mypackage.imported_m.X) # revealed: Unknown
456473
```
457474

458-
## Relative `from` Import of Direct Submodule in `__init__`, Mismatched Alias (Non-Stub Check)
475+
### In Non-Stub
459476

460477
`mypackage/__init__.py`:
461478

@@ -485,6 +502,8 @@ reveal_type(mypackage.imported_m.X) # revealed: int
485502
The `__init__.pyi` idiom should definitely always work if the submodule is renamed to itself, as
486503
this is the re-export idiom.
487504

505+
### In Stub
506+
488507
`mypackage/__init__.pyi`:
489508

490509
```pyi
@@ -505,7 +524,7 @@ import mypackage
505524
reveal_type(mypackage.imported.X) # revealed: int
506525
```
507526

508-
## Relative `from` Import of Direct Submodule in `__init__`, Matched Alias (Non-Stub Check)
527+
### In Non-Stub
509528

510529
`mypackage/__init__.py`:
511530

@@ -532,6 +551,8 @@ reveal_type(mypackage.imported.X) # revealed: int
532551
Even if the `__init__` idiom is in effect, star imports do not pick it up. (This is an arbitrary
533552
decision that mostly fell out of the implementation details and can be changed!)
534553

554+
### In Stub
555+
535556
`mypackage/__init__.pyi`:
536557

537558
```pyi
@@ -556,7 +577,7 @@ reveal_type(imported.X) # revealed: Unknown
556577
reveal_type(Z) # revealed: int
557578
```
558579

559-
## Star Import Unaffected (Non-Stub Check)
580+
### In Non-Stub
560581

561582
`mypackage/__init__.py`:
562583

@@ -586,6 +607,8 @@ reveal_type(Z) # revealed: int
586607
A `from` import that imports a non-submodule isn't currently a special case here (various
587608
proposed/tested approaches did treat this specially).
588609

610+
### In Stub
611+
589612
`mypackage/__init__.pyi`:
590613

591614
```pyi
@@ -607,7 +630,7 @@ import mypackage
607630
reveal_type(mypackage.imported.X) # revealed: Unknown
608631
```
609632

610-
## `from` Import of Non-Submodule (Non-Stub Check)
633+
### In Non-Stub
611634

612635
`mypackage/__init__.py`:
613636

@@ -634,6 +657,8 @@ reveal_type(mypackage.imported.X) # revealed: int
634657
`from mypackage import submodule` from outside the package is not modeled as a side-effect on
635658
`mypackage`, even in the importing file (this could be changed!).
636659

660+
### In Stub
661+
637662
`mypackage/__init__.pyi`:
638663

639664
```pyi
@@ -658,7 +683,7 @@ reveal_type(imported.X) # revealed: int
658683
reveal_type(mypackage.imported.X) # revealed: Unknown
659684
```
660685

661-
## `from` Import of Other Package's Submodule (Non-Stub Check)
686+
### In Non-Stub
662687

663688
`mypackage/__init__.py`:
664689

@@ -688,6 +713,8 @@ reveal_type(mypackage.imported.X) # revealed: Unknown
688713
`from . import submodule` from a sibling module is not modeled as a side-effect on `mypackage` or a
689714
re-export from `submodule`.
690715

716+
### In Stub
717+
691718
`mypackage/__init__.pyi`:
692719

693720
```pyi
@@ -719,7 +746,7 @@ reveal_type(imported.fails.Y) # revealed: Unknown
719746
reveal_type(mypackage.fails.Y) # revealed: Unknown
720747
```
721748

722-
## `from` Import of Sibling Module (Non-Stub Check)
749+
### In Non-Stub
723750

724751
`mypackage/__init__.py`:
725752

@@ -768,6 +795,8 @@ We avoid this by ensuring that the imported name (the right-hand `funcmod` in
768795
`from .funcmod import funcmod`) overwrites the submodule attribute (the left-hand `funcmod`), as it
769796
does at runtime.
770797

798+
### In Stub
799+
771800
`mypackage/__init__.pyi`:
772801

773802
```pyi
@@ -800,7 +829,7 @@ from mypackage import funcmod
800829
x = funcmod(1)
801830
```
802831

803-
## Fractal Re-export Nameclash Problems (Non-Stub Check)
832+
### In Non-Stub
804833

805834
`mypackage/__init__.py`:
806835

@@ -835,7 +864,7 @@ from mypackage import funcmod
835864
x = funcmod(1)
836865
```
837866

838-
## Re-export Nameclash Problems In Functions (Non-Stub Check)
867+
## Re-export Nameclash Problems In Functions
839868

840869
`from` imports in an `__init__.py` at file scope should be visible to functions defined in the file:
841870

@@ -857,7 +886,7 @@ def funcmod(x: int) -> int:
857886
return x
858887
```
859888

860-
## Re-export Nameclash Problems In Try-Blocks (Non-Stub Check)
889+
## Re-export Nameclash Problems In Try-Blocks
861890

862891
`from` imports in an `__init__.py` at file scope in a `try` block should be visible to functions
863892
defined in the `try` block (regression test for a bug):
@@ -886,7 +915,7 @@ def funcmod(x: int) -> int:
886915
return x
887916
```
888917

889-
## RHS `from` Imports In Functions (Non-Stub Check)
918+
## RHS `from` Imports In Functions
890919

891920
If a `from` import occurs in a function, the RHS symbols should only be visible in that function.
892921

@@ -918,7 +947,7 @@ def funcmod(x: int) -> int:
918947
return x
919948
```
920949

921-
## LHS `from` Imports In Functions (Non-Stub Check)
950+
## LHS `from` Imports In Functions
922951

923952
If a `from` import occurs in a function, LHS symbols should only be visible in that function. This
924953
very blatantly is not runtime-accurate, but exists to try to force you to write "obviously
@@ -957,7 +986,7 @@ def funcmod(x: int) -> int:
957986
return x
958987
```
959988

960-
## LHS `from` Imports Overwrite Locals (Non-Stub Check)
989+
## LHS `from` Imports Overwrite Locals
961990

962991
The LHS of a `from..import` introduces a local symbol that overwrites any local with the same name.
963992
This reflects actual runtime behaviour, although we're kinda assuming it hasn't been imported
@@ -984,6 +1013,8 @@ def funcmod(x: int) -> int:
9841013
The LHS of a `from..import` introduces a local symbol that can be overwritten by defining a function
9851014
(or class) with the same name.
9861015

1016+
### In Stub
1017+
9871018
`mypackage/__init__.pyi`:
9881019

9891020
```pyi
@@ -1006,7 +1037,7 @@ from mypackage import funcmod
10061037
x = funcmod(1)
10071038
```
10081039

1009-
## LHS `from` Imports Overwritten By Local Function (Non-Stub Check)
1040+
### In Non-Stub
10101041

10111042
`mypackage/__init__.py`:
10121043

@@ -1036,6 +1067,8 @@ x = funcmod(1)
10361067

10371068
The LHS of a `from..import` introduces a local symbol that can be overwritten by assigning to it.
10381069

1070+
### In Stub
1071+
10391072
`mypackage/__init__.pyi`:
10401073

10411074
```pyi
@@ -1058,7 +1091,7 @@ from mypackage import funcmod
10581091
x = funcmod(1)
10591092
```
10601093

1061-
## LHS `from` Imports Overwritten By Local Assignment (Non-Stub Check)
1094+
### In Non-Stub
10621095

10631096
`mypackage/__init__.py`:
10641097

@@ -1088,6 +1121,8 @@ x = funcmod(1)
10881121
The LHS of a `from..import` of a submodule introduces a local symbol only the first time it
10891122
introduces a direct submodule. The second time does nothing.
10901123

1124+
### In Stub
1125+
10911126
`mypackage/__init__.pyi`:
10921127

10931128
```pyi
@@ -1110,7 +1145,7 @@ from mypackage import funcmod
11101145
x = funcmod(1)
11111146
```
11121147

1113-
## LHS `from` Imports Only Apply The First Time (Non-Stub Check)
1148+
### In Non-Stub
11141149

11151150
`mypackage/__init__.py`:
11161151

0 commit comments

Comments
 (0)