Skip to content

One import statement per module - #75

Merged
Paebbels merged 2 commits into
devfrom
claude/one-import-statement
Oct 25, 2026
Merged

Paebbels merged 2 commits into
devfrom
claude/one-import-statement

Conversation

@pytooling-claude

Copy link
Copy Markdown

Changes

  • One from <module> import ... statement per module and scope: several statements of one module are merged into one, continued by \ and aligned to the column of the first imported name - also inside if TYPE_CHECKING: blocks (tab, then spaces) and function bodies.
    • pyTooling.Sphinx: HTML, LaTeX, Node at module level; Abbreviation, CodeCoverage, DocCoverage, ExternalLink, Roles, Unittest in setup().
    • pyTooling.Sphinx.CodeCoverage: pyTooling.Sphinx.
    • pyTooling.Sphinx.CondensedClass: ast.
    • pyTooling.Sphinx.CondensedSource: ast, typing.
    • pyTooling.Sphinx.DependencyTable: typing, pyTooling.Sphinx, pyTooling.Dependency.Python (under if TYPE_CHECKING:).
    • pyTooling.Sphinx.DocCoverage: typing, pyTooling.Sphinx.
    • pyTooling.Sphinx.HTML, pyTooling.Sphinx.LaTeX, pyTooling.Sphinx.Tree: pyTooling.Sphinx.Node.
    • pyTooling.Sphinx.Unittest: typing, pyTooling.Sphinx, pyEDAA.Reports.Unittesting (under if TYPE_CHECKING:).
  • Each extra statement runs IMPORT_NAME once more: about 0.75 µs on Python 3.15 and 0.43 µs on 3.13 per statement. The package has 403 → 358 import statements.
  • Imported names, the order in which modules are first imported, and the namespace of every module are unchanged (compared before/after).
  • pyTooling.Sphinx.DependencyTable: DependencyError (3 functions), UnknownLicense (DependencyTable._LicenseName) and SPDXLicense (DependencyTable._LicenseURL) are imported at module level instead of inside the functions. The module's own imports load pyTooling.Dependency and, through it, pyTooling.Licensing anyway; _LicenseName and _LicenseURL ran their import once per table row. The first-import order of all modules is unchanged. The package then has 354 import statements.
  • Kept function-local, on purpose:
    • Optional dependencies, so the extension works without them: pyEDAA.Reports (extra reports; CodeCoverage, CondensedSource, DocCoverage, Unittest), markdown-it-py and mdit-py-plugins (extra markdown; GitHubMarkdown), requests and packaging.utils (behind MissingDependencyError for the pypi extra; DependencyTable).
    • Lazily loaded modules, not loaded by importing the directive's module: pyTooling.Dependency.Python and pyTooling.Dependency.Resolver (DependencyTable; also gated by if TYPE_CHECKING: at module level), xmlschema (XMLSchemaGraph).
    • Cycles: CodeCoveragePages and UnittestPages import CodeCoverage and Unittest, which use them inside methods.
    • setup(), readReports() and checkReportConfiguration() in pyTooling.Sphinx: the submodules import pyTooling.Sphinx, and setup() documents why it imports them.

Unit Tests

  • Same rule in tests/unit: 171 → 167 import statements (DependencyTable, Download, Extension, ExternalLink).
  • DEFAULT_VERSION_FORMAT and DEFAULT_DEPENDENCY_FORMAT (module already imported by tests/unit/DependencyTable.py) and pygments.token (loaded with Sphinx; tests/unit/CodeCoveragePages.py) are imported at module level instead of inside a testcase: 165 import statements. pyEDAA.Reports stays a local import in tests/unit/DocCoverage.py.

Related Issues and Pull-Requests

claude-code and others added 2 commits October 24, 2026 12:19
Several `from <module> import ...` statements of one module in one scope (module level, `if TYPE_CHECKING:`,
function body) are merged into one statement at the position of the first, continued by `\` and aligned to the
first name's column. Each extra statement runs `IMPORT_NAME` once more (about 0.75 us on Python 3.15, 0.43 us on
3.13).

pyTooling.Sphinx: 403 -> 358 import statements, tests: 171 -> 167. Imported names, the order in which modules are first imported and
every module's namespace are unchanged (compared before/after).

Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
`pyTooling.Sphinx.DependencyTable` imported `DependencyError` (3x), `UnknownLicense` and `SPDXLicense` inside
functions, while its module-level imports load `pyTooling.Dependency` and, through it, `pyTooling.Licensing` anyway.
`_LicenseName` and `_LicenseURL` ran their import once per table row. The names join the module's import block; the
first-import order of all modules is unchanged.

Tests: `DEFAULT_VERSION_FORMAT`, `DEFAULT_DEPENDENCY_FORMAT` (module already imported by the test file) and
`pygments.token` (loaded with Sphinx) move to the module level too.

Kept function-local: imports of optional dependencies (pyEDAA.Reports, markdown-it-py, `requests`, `packaging`),
lazily loaded modules (`pyTooling.Dependency.Python`/`.Resolver`, `xmlschema`), the cyclic imports of the report
pages modules, and the submodule imports in `setup()`, `readReports()` and `checkReportConfiguration()`.

pyTooling.Sphinx: 358 -> 354 import statements, tests: 167 -> 165.

Co-Authored-By: Patrick Lehmann <Paebbels@gmail.com>
@pytooling-claude
pytooling-claude Bot requested a review from Paebbels as a code owner October 25, 2026 02:03
@codacy-production

codacy-production Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

🟢 Coverage 100.00% diff coverage · -0.04% coverage variation

Metric Results
Coverage variation ✅ -0.04% coverage variation (-1.00%)
Diff coverage ✅ 100.00% diff coverage

View coverage diff in Codacy

Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (ddeba52) 4899 4304 87.85%
Head commit (bf1be1a) 4852 (-47) 4261 (-43) 87.82% (-0.04%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#75) 24 24 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.57%. Comparing base (ddeba52) to head (bf1be1a).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev      #75      +/-   ##
==========================================
- Coverage   85.62%   85.57%   -0.06%     
==========================================
  Files          24       24              
  Lines        4899     4852      -47     
  Branches      779      779              
==========================================
- Hits         4195     4152      -43     
+ Misses        595      591       -4     
  Partials      109      109              
Flag Coverage Δ
unittests 85.57% <100.00%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Paebbels
Paebbels merged commit fa58730 into dev Oct 25, 2026
58 of 59 checks passed
@Paebbels
Paebbels deleted the claude/one-import-statement branch October 25, 2026 10:00
This was referenced Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants