Skip to content

Commit

Permalink
Merge pull request #8 from Sachaa-Thanasius/bugfix/defer_module_level…
Browse files Browse the repository at this point in the history
…-not-respected

Fix defer_module_level not being respected within _DeferredFileLoader.
  • Loading branch information
Sachaa-Thanasius authored Oct 29, 2024
2 parents b438067 + eeaf6f8 commit 025245a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: check-yaml

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
rev: v0.7.1
hooks:
- id: ruff
args: [--fix]
Expand Down
16 changes: 7 additions & 9 deletions bench/bench_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,19 @@ def bench_slothy() -> float:


def bench_defer_imports_local() -> float:
with CatchTime() as ct:
hook_ctx = defer_imports.install_import_hook()
with defer_imports.install_import_hook(uninstall_after=True), CatchTime() as ct:
import bench.sample_defer_local
hook_ctx.uninstall()
return ct.elapsed


def bench_defer_imports_global() -> float:
with CatchTime() as ct:
hook_ctx = defer_imports.install_import_hook(apply_all=True)
with defer_imports.install_import_hook(uninstall_after=True, apply_all=True), CatchTime() as ct:
import bench.sample_defer_global
hook_ctx.uninstall()
return ct.elapsed


def remove_pycaches() -> None:
"""Remove all cached Python bytecode files from the current working directory."""
"""Remove all cached Python bytecode files from the current directory."""

for dir_ in Path().rglob("__pycache__"):
shutil.rmtree(dir_)
Expand Down Expand Up @@ -119,11 +115,13 @@ def main() -> None:
import argparse

parser = argparse.ArgumentParser()

default_exec_order = list(BENCH_FUNCS)
parser.add_argument(
"--exec-order",
action="extend",
nargs=4,
choices=BENCH_FUNCS.keys(),
choices=default_exec_order,
type=str,
help="The order in which the influenced (or not influenced) imports are run",
)
Expand All @@ -132,7 +130,7 @@ def main() -> None:
if sys.dont_write_bytecode:
remove_pycaches()

exec_order: list[str] = args.exec_order or list(BENCH_FUNCS)
exec_order: list[str] = args.exec_order or default_exec_order

results = {type_: BENCH_FUNCS[type_]() for type_ in exec_order}
minimum = min(results.values())
Expand Down
22 changes: 15 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,16 @@ select = [
"UP",
"YTT",
"ANN",
"ASYNC",
"S",
"BLE",
# "FBT",
"B",
"A",
"COM",
"C4",
"DTZ",
"T10",
"EM",
"ISC",
"G",
Expand All @@ -122,10 +125,12 @@ select = [
"ERA",
"PL",
"TRY",
"PERF",
"FURB",
"RUF",
]
extend-ignore = [
# ---- General ignores
# ---- General
"S101", # Use of assert here is a known quantity for typing cases. All uses should be safe to optimize out.
"SIM105", # Suppressable exception. contextlib.suppress is a stylistic choice with overhead.
"C90", # McCabe complexity.
Expand All @@ -145,9 +150,9 @@ extend-ignore = [
"E111",
"E114",
"E117",
"E501",
"COM812",
"COM819",
"E501",
"ISC001",
"ISC002",

Expand All @@ -174,22 +179,25 @@ keep-runtime-typing = true

# ---- Test code
"tests/**/test_*.py" = [
"T201", # Printing is fine.
"T203", # Pretty-printing is fine.
# Don't need return annotations in tests.
"ANN201",
"ANN202",
"T201", # Printing is fine.
"T203", # Pretty-printing is fine.
"ANN201", # Don't need return annotations in tests.
"ANN202", # Don't need return annotations in tests.
"S102", # exec is used to test for NameError within a module's namespace.
]
"tests/sample_stdlib_imports.py" = [
"F401", # Unused imports are fine; we're testing import success.
"ERA001", # Plenty of imports are commented out with explanations next to them.
"T100", # Importing pdb is fine.
]
"bench/**/*.py" = [
"T201", # Printing is fine.
"F401", # Unused imports are fine; we're testing import speed.
"ERA001", # Plenty of imports are commented out with explanations next to them.
]
"bench/**/sample_*.py" = [
"T100", # Importing pdb is fine.
]


# -------- Type-checker config
Expand Down
Loading

0 comments on commit 025245a

Please sign in to comment.