Skip to content

Commit

Permalink
Prevent changes to only the README from triggerin CI.
Browse files Browse the repository at this point in the history
- Clean up the lint rules slightly.
- Attempt to fix the blockquotes in the README again.
  • Loading branch information
Sachaa-Thanasius committed Aug 26, 2024
1 parent fccb50b commit 35afc25
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 49 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ name: lint

on:
push:
paths-ignore:
- 'README.rst'
pull_request:
types: [ opened, synchronize ]
paths-ignore:
- 'README.rst'
workflow_dispatch:

jobs:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ name: test

on:
push:
paths-ignore:
- 'README.rst'
pull_request:
types: [ opened, synchronize ]
paths-ignore:
- 'README.rst'
workflow_dispatch:

jobs:
Expand All @@ -15,7 +19,7 @@ jobs:
python-version: [ '3.9', '3.10', '3.11', '3.12', '3.13', 'pypy-3.9', 'pypy-3.10' ]
os: [ubuntu-latest, macOS-latest, windows-latest]

name: check ${{ matrix.python-version }}
name: check ${{ matrix.python-version }} on ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
Expand Down
54 changes: 28 additions & 26 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ Example
from typing import TypeVar
# inspect and TypeVar won't be imported until referenced.
# For imports that are only used for annotations, this import cost can be avoided entirely by making sure all annotations are strings.
# For imports that are only used for annotations,
# this import cost can be avoided entirely by making sure
# all annotations are strings.
Setup
Expand All @@ -66,19 +68,19 @@ See this README as well as docstrings and comments in the code.
Features and Caveats
--------------------

- Python implementation–agnostic, in theory
- Python implementation–agnostic, in theory

- The only dependency is on locals() at module scope to maintain its current API: specifically, that its return value will be a read-through, *write-through*, dict-like view of the module locals.
- The only dependency is on locals() at module scope to maintain its current API: specifically, that its return value will be a read-through, *write-through*, dict-like view of the module locals.

- Not that slow, especially with support from bytecode caching
- Doesn't support lazy importing in class or function scope
- Doesn't support wildcard imports
- Not that slow, especially with support from bytecode caching
- Doesn't support lazy importing in class or function scope
- Doesn't support wildcard imports


Benchmarks
==========

The methodology is somewhat rough: at the moment, time to import is being measured with both the ``benchmark/bench_samples.py`` script (run with ``python -m benchmark.bench_samples``) and python's importtime command-line function (e.g. run with ``python -X importtime -c "import deferred``).
The methodology is somewhat rough: at the moment, time to import is being measured with both the ``benchmark/bench_samples.py`` script (run with ``python -m benchmark.bench_samples``) and python's importtime command-line function (e.g. run with ``python -X importtime -c "import deferred"``).


Why?
Expand All @@ -90,12 +92,12 @@ I wasn't satisfied with the state of lazy imports in Python and wanted to put my
TODO
====

- [x] Investigate if this package would benefit from a custom optimization suffix for bytecode. UPDATE: Added in a different way without monkeypatching, thanks to `this blog post <https://gregoryszorc.com/blog/2017/03/13/from-__past__-import-bytes_literals/>`_.
- [x] Investigate if this package would benefit from a custom optimization suffix for bytecode. UPDATE: Added in a different way without monkeypatching, thanks to `this blog post <https://gregoryszorc.com/blog/2017/03/13/from-__past__-import-bytes_literals/>`_.

- Signs point to yes, but I'm not a fan of the monkeypatching seemingly involved, nor of having to import ``importlib.util``.
- See beartype and its justification `for <https://github.com/beartype/beartype/blob/e9eeb4e282f438e770520b99deadbe219a1c62dc/beartype/claw/_importlib/_clawimpload.py#L177-L312>`_ `this <https://github.com/beartype/beartype/blob/e9eeb4e282f438e770520b99deadbe219a1c62dc/beartype/claw/_importlib/clawimpcache.py#L22-L26>`_.
- Signs point to yes, but I'm not a fan of the monkeypatching seemingly involved, nor of having to import ``importlib.util``.
- See beartype and its justification `for <https://github.com/beartype/beartype/blob/e9eeb4e282f438e770520b99deadbe219a1c62dc/beartype/claw/_importlib/_clawimpload.py#L177-L312>`_ `this <https://github.com/beartype/beartype/blob/e9eeb4e282f438e770520b99deadbe219a1c62dc/beartype/claw/_importlib/clawimpcache.py#L22-L26>`_.

- [x] Fix subpackage imports being broken if done within ``defer_imports_until_use`` like this:
- [x] Fix subpackage imports being broken if done within ``defer_imports_until_use`` like this:

.. code:: python
Expand All @@ -106,28 +108,28 @@ TODO
import importlib.abc
import importlib.util
- One remaining problem: I don't know why it works, just some of how.
- One remaining problem: I don't know why it works, just some of how.

- [ ] Add tests for the following:
- [ ] Add tests for the following:

- [x] Relative imports
- [x] Combinations of different import types
- [x] Circular imports
- [ ] Thread safety (see importlib.util.LazyLoader for reference?)
- [x] Other python implementations/platforms
- [x] Relative imports
- [x] Combinations of different import types
- [x] Circular imports
- [ ] Thread safety (see importlib.util.LazyLoader for reference?)
- [x] Other python implementations/platforms

- [x] Make this able to import the entire standard library, including all the subpackage imports uncommented. UPDATE: See ``benchmark/sample_deferred.py``.
- [x] Make this be able to run on normal code. It currently breaks pip, readline, and who knows what else in the standard library, possibly because of the subpackage imports issue.
- [ ] Investigate remaining TODO comments in the code.
- [x] Make this able to import the entire standard library, including all the subpackage imports uncommented. UPDATE: See ``benchmark/sample_deferred.py``.
- [x] Make this be able to run on normal code. It currently breaks pip, readline, and who knows what else in the standard library, possibly because of the subpackage imports issue.
- [ ] Investigate remaining TODO comments in the code.


Acknowledgements
================

- Thanks to PEP 690 for pushing this feature and two pure-Python pieces of code for serving as starting points and references.
- Thanks to PEP 690 for pushing this feature and two pure-Python pieces of code for serving as starting points and references.

- `PEP 690 <https://peps.python.org/pep-0690/>`_
- `Jelle's lazy gist <https://gist.github.com/JelleZijlstra/23c01ceb35d1bc8f335128f59a32db4c>`_
- `slothy <https://github.com/bswck/slothy>`_ (based on the previous gist)
- `PEP 690 <https://peps.python.org/pep-0690/>`_
- `Jelle's lazy gist <https://gist.github.com/JelleZijlstra/23c01ceb35d1bc8f335128f59a32db4c>`_
- `slothy <https://github.com/bswck/slothy>`_ (based on the previous gist)

- Thanks to Sinbad for the feedback and for unintentionally pushing me towards this approach.
- Thanks to Sinbad for the feedback and for unintentionally pushing me towards this approach.
17 changes: 6 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]

[project.optional-dependencies]
dev = ["typing-extensions", "pytest", "coverage", "covdefaults"]
dev = ["pytest", "coverage", "covdefaults"]
benchmark = ["slothy"]

[project.urls]
Expand Down Expand Up @@ -133,12 +133,6 @@ extend-ignore = [
"ISC001",
"ISC002",
# ---- Project-specific rules
# -- Readability
# Allow else and elif after return, raise, and continue.
"RET505",
"RET506",
"RET507",
"SIM108", # Don't shorten if-else to ternary.
]
unfixable = [
"ERA", # Prevent unlikely erroneous deletion.
Expand All @@ -159,9 +153,7 @@ keep-runtime-typing = true
"F405",
]
"src/deferred/**/*.py" = [
# Allow some builtin name shadowing.
"A001",
"A002",
"A002", # Allow some shadowing of builtins by parameter names.
]
# ---- Test code
"tests/**/*.py" = [
Expand All @@ -170,7 +162,10 @@ keep-runtime-typing = true
"ANN201", # Don't need return annotations for tests.
"S102", # exec is used to test for NameError within a module's namespace.
]
"benchmark/**/*.py" = ["F401", "ERA001"]
"benchmark/**/*.py" = [
"F401", # Allow unused imports; we're testing import speed.
"ERA001", # Allow commented code.
]


# -------- Type-checking config
Expand Down
25 changes: 14 additions & 11 deletions src/deferred/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,27 +516,30 @@ def calc___package__(globals: dict[str, _tp.Any]) -> _tp.Optional[str]:

package: str | None = globals.get("__package__")
spec: ModuleSpec | None = globals.get("__spec__")

# TODO: Keep the warnings in sync with supported CPython versions.
if package is not None:
if spec is not None and package != spec.parent:
# TODO: Keep the warnings up to date with CPython.
category = DeprecationWarning if sys.version_info >= (3, 12) else ImportWarning
warnings.warn(
f"__package__ != __spec__.parent ({package!r} != {spec.parent!r})",
category,
stacklevel=3,
)
return package
elif spec is not None:

if spec is not None:
return spec.parent
else:
warnings.warn(
"can't resolve package from __spec__ or __package__, falling back on __name__ and __path__",
ImportWarning,
stacklevel=3,
)
package = globals["__name__"]
if "__path__" not in globals:
package = package.rpartition(".")[0] # pyright: ignore [reportOptionalMemberAccess]

warnings.warn(
"can't resolve package from __spec__ or __package__, falling back on __name__ and __path__",
ImportWarning,
stacklevel=3,
)
package = globals["__name__"]
if "__path__" not in globals:
package = package.rpartition(".")[0] # pyright: ignore [reportOptionalMemberAccess]

return package


Expand Down

0 comments on commit 35afc25

Please sign in to comment.