Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
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
1c691ea3b0d7459eef03cde281fd41ab092058bc6e6d7c87af992ee76a9113203a419ae0878bef9880bff3f579544a3e4673de8d007a633f20b14d9164423b00e7800781eedb9be59ecb179da29078f57f67310c39c33dfc8c88387c494055d6026ac0c0d805ff0dd2214b254e5e9592aa85f9d2799a4d7d07ae1c63198c46b3b75c5efb200c246bcd9ad012fe526b406b9110ee77665a3ddde4cdec6a6c3b48072e197652af21a134ea0a55166d39a0a91841f6518d90246cc824ac88c075f5b59110fcaeda7ac8d57acad8f95f7b743eb0e6cb1310409481617e9d1db151a5ea120fc973a4fa266fd4d441602ae6633ff2f642c8ab07b1476846fe0b36549971d3950c019fd8e1b20a70e5ce7a39cd25f70d4df510c200d58b0cf8e4f2924cd43222d3681e4cc69054f675fd4c3477b2f7d2230985e2a610af78917b92ee777866952ac2133dc19e80133a5b0adc30470b9e441761e574b3efe006ca9cb5121b0093f08b2a514d974f35c43014816f96a99cc4ed3c45000033a0a28c2023f8064b352dc7a0ddfd6fd8c59a05b50dac80f2d2d4bdcccd1878c31b7fe9edf9e754fd8fe85e4c90aca0899c07c173811e5dca9c6757c7ef77371cc816bb3060f6909b69a7995d56d95fd853a535de1572241f2adb049bcbe0a9715124578dbfd002276a9d6c8e8366ebd249712b7c24d68f4d1129b931d1c1d4196dfb437afb9fc7da229bd6c4eaf8335b90e3ea8639e5037e8863b628adf1f67a542abd71872a7111b960d3a9dc236e05c1f854ce061e99dae608fd3f20aa2f4defad3ae20c9b6a280b3925File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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 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. | ||
| Lazy imports are only permitted at module scope. Using :keyword:`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 resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
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.