-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
gh-142349: Implement PEP 810 #142351
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
base: main
Are you sure you want to change the base?
gh-142349: Implement PEP 810 #142351
Changes from all commits
1c691ea
3b0d745
9eef03c
de281fd
41ab092
058bc6e
6d7c87a
f992ee7
6a91132
03a419a
e0878be
f9880bf
f3f5795
44a3e46
73de8d0
07a633f
20b14d9
164423b
00e7800
781eedb
9be59ec
b179da2
9078f57
f67310c
39c33df
c8c8838
7c49405
5d6026a
c0c0d80
5ff0dd2
214b254
e5e9592
aa85f9d
2799a4d
7d07ae1
c63198c
46b3b75
c5efb20
0c246bc
d9ad012
fe526b4
06b9110
ee77665
a3ddde4
cdec6a6
c3b4807
2e19765
2af21a1
34ea0a5
5166d39
a0a9184
1f6518d
90246cc
824ac88
c075f5b
59110fc
aeda7ac
8d57aca
d8f95f7
b743eb0
e6cb131
0409481
617e9d1
db151a5
ea120fc
973a4fa
266fd4d
441602a
e6633ff
2f642c8
ab07b14
76846fe
0b36549
971d395
0c019fd
8e1b20a
70e5ce7
a39cd25
f70d4df
510c200
d58b0cf
8e4f292
4cd4322
2d3681e
4cc6905
4f675fd
4c3477b
2f7d223
0985e2a
610af78
917b92e
e777866
952ac21
33dc19e
80133a5
b0adc30
470b9e4
41761e5
74b3efe
006ca9c
b5121b0
093f08b
2a514d9
74f35c4
3014816
f96a99c
c4ed3c4
5000033
a0a28c2
023f806
4b352dc
7a0ddfd
6fd8c59
a05b50d
ac80f2d
2d4bdcc
cd1878c
31b7fe9
edf9e75
4fd8fe8
5e4c90a
ca0899c
07c1738
11e5dca
9c6757c
7ef7737
1cc816b
b3060f6
909b69a
7995d56
d95fd85
3a535de
1572241
f2adb04
9bcbe0a
9715124
578dbfd
002276a
9d6c8e8
366ebd2
49712b7
c24d68f
4d1129b
931d1c1
d4196df
b437afb
9fc7da2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -748,14 +748,15 @@ The :keyword:`!import` statement | |||||||||||||||||||||||||||||||||
| pair: name; binding | ||||||||||||||||||||||||||||||||||
| pair: keyword; from | ||||||||||||||||||||||||||||||||||
| pair: keyword; as | ||||||||||||||||||||||||||||||||||
| pair: keyword; lazy | ||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest you move this to the below section on lazy specifically. |
||||||||||||||||||||||||||||||||||
| pair: exception; ImportError | ||||||||||||||||||||||||||||||||||
| single: , (comma); import statement | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. productionlist:: python-grammar | ||||||||||||||||||||||||||||||||||
| import_stmt: "import" `module` ["as" `identifier`] ("," `module` ["as" `identifier`])* | ||||||||||||||||||||||||||||||||||
| : | "from" `relative_module` "import" `identifier` ["as" `identifier`] | ||||||||||||||||||||||||||||||||||
| import_stmt: ["lazy"] "import" `module` ["as" `identifier`] ("," `module` ["as" `identifier`])* | ||||||||||||||||||||||||||||||||||
| : | ["lazy"] "from" `relative_module` "import" `identifier` ["as" `identifier`] | ||||||||||||||||||||||||||||||||||
| : ("," `identifier` ["as" `identifier`])* | ||||||||||||||||||||||||||||||||||
| : | "from" `relative_module` "import" "(" `identifier` ["as" `identifier`] | ||||||||||||||||||||||||||||||||||
| : | ["lazy"] "from" `relative_module` "import" "(" `identifier` ["as" `identifier`] | ||||||||||||||||||||||||||||||||||
| : ("," `identifier` ["as" `identifier`])* [","] ")" | ||||||||||||||||||||||||||||||||||
| : | "from" `relative_module` "import" "*" | ||||||||||||||||||||||||||||||||||
| module: (`identifier` ".")* `identifier` | ||||||||||||||||||||||||||||||||||
|
|
@@ -870,6 +871,57 @@ determine dynamically the modules to be loaded. | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. audit-event:: import module,filename,sys.path,sys.meta_path,sys.path_hooks import | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. _lazy-imports: | ||||||||||||||||||||||||||||||||||
| .. _lazy: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Lazy imports | ||||||||||||||||||||||||||||||||||
| ------------ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. index:: | ||||||||||||||||||||||||||||||||||
| pair: lazy; import | ||||||||||||||||||||||||||||||||||
| single: lazy import | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| The :keyword:`lazy` keyword marks an import as lazy. It is a :ref:`soft keyword | ||||||||||||||||||||||||||||||||||
| <soft-keywords>` that only has special meaning when it appears immediately | ||||||||||||||||||||||||||||||||||
| before an :keyword:`import` or :keyword:`from` statement. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| When an import statement is preceded by the :keyword:`lazy` keyword, | ||||||||||||||||||||||||||||||||||
| the import becomes *lazy*: the module is not loaded immediately at the import | ||||||||||||||||||||||||||||||||||
| statement. Instead, a lazy proxy object is created and bound to the name. The | ||||||||||||||||||||||||||||||||||
| actual module is loaded on first use of that name. | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+885
to
+892
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would recommend swapping the first two paragraphs around, the first sentence "the lazy keyword marks an import as lazy" is not super helpful if you don't define what you mean by "lazy" until the next paragraph 😅
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternatively, you could keep the same order of the paragraphs but just remove the first sentence |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Lazy imports are only permitted at module scope. Using ``lazy`` inside a | ||||||||||||||||||||||||||||||||||
| function, class body, or :keyword:`try`/:keyword:`except`/:keyword:`finally` | ||||||||||||||||||||||||||||||||||
| block raises a :exc:`SyntaxError`. Star imports cannot be lazy (``lazy from | ||||||||||||||||||||||||||||||||||
| module import *`` is a syntax error), and :ref:`future statements <future>` | ||||||||||||||||||||||||||||||||||
| cannot be lazy. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| When using ``lazy from ... import``, each imported name is bound to a lazy | ||||||||||||||||||||||||||||||||||
| proxy object. The first access to any of these names triggers loading of the | ||||||||||||||||||||||||||||||||||
| entire module and resolves only that specific name to its actual value. Other | ||||||||||||||||||||||||||||||||||
| names remain as lazy proxies until they are accessed. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Example:: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| lazy import json | ||||||||||||||||||||||||||||||||||
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| print('json' in sys.modules) # False - json module not yet loaded | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # First use triggers loading | ||||||||||||||||||||||||||||||||||
| result = json.dumps({"hello": "world"}) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| print('json' in sys.modules) # True - now loaded | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| If an error occurs during module loading (such as :exc:`ImportError` or | ||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps also document that ImportCycleError may be raised? |
||||||||||||||||||||||||||||||||||
| :exc:`SyntaxError`), it is raised at the point where the lazy import is first | ||||||||||||||||||||||||||||||||||
| used, not at the import statement itself. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| See :pep:`810` for the full specification of lazy imports. | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. versionadded:: next | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| .. _future: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| Future statements | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copy-paste error? This seems unrelated to the function being described.