Skip to content

Commit 0c04f4e

Browse files
henryiiiKludex
andauthored
fix: use alternate scheme for importing multipart (#168)
Co-authored-by: Marcelo Trylesinski <[email protected]>
1 parent 72e30ea commit 0c04f4e

File tree

12 files changed

+59
-51
lines changed

12 files changed

+59
-51
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ jobs:
3434
run: scripts/test
3535

3636
- name: Run rename test
37-
run: uvx nox -s rename -P ${{ matrix.python-version }}
38-
37+
run: scripts/rename
3938
# https://github.com/marketplace/actions/alls-green#why used for branch protection checks
4039
check:
4140
if: always()

_python_multipart.pth

Lines changed: 0 additions & 1 deletion
This file was deleted.

_python_multipart_loader.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

multipart/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This only works if using a file system, other loaders not implemented.
2+
3+
import importlib.util
4+
import sys
5+
import warnings
6+
from pathlib import Path
7+
8+
for p in sys.path:
9+
file_path = Path(p, "multipart.py")
10+
if file_path.is_file():
11+
spec = importlib.util.spec_from_file_location("multipart", file_path)
12+
assert spec is not None, f"{file_path} found but not loadable!"
13+
module = importlib.util.module_from_spec(spec)
14+
sys.modules["multipart"] = module
15+
assert spec.loader is not None, f"{file_path} must be loadable!"
16+
spec.loader.exec_module(module)
17+
break
18+
else:
19+
warnings.warn("Please use `import python_multipart` instead.", FutureWarning, stacklevel=2)
20+
from python_multipart import *

multipart/decoders.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from python_multipart.decoders import *

multipart/exceptions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from python_multipart.exceptions import *

multipart/multipart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from python_multipart.multipart import *

noxfile.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1+
import inspect
2+
13
import nox
24

35
nox.needs_version = ">=2024.4.15"
46
nox.options.default_venv_backend = "uv|virtualenv"
57

6-
ALL_PYTHONS = [
7-
c.split()[-1]
8-
for c in nox.project.load_toml("pyproject.toml")["project"]["classifiers"]
9-
if c.startswith("Programming Language :: Python :: 3.")
10-
]
11-
128

13-
@nox.session(python=ALL_PYTHONS)
9+
@nox.session
1410
def rename(session: nox.Session) -> None:
1511
session.install(".")
1612
assert "import python_multipart" in session.run("python", "-c", "import multipart", silent=True)
@@ -27,3 +23,21 @@ def rename(session: nox.Session) -> None:
2723
assert "import python_multipart" not in session.run(
2824
"python", "-c", "import python_multipart; python_multipart.parse_form", silent=True
2925
)
26+
27+
28+
@nox.session
29+
def rename_inline(session: nox.Session) -> None:
30+
session.install("pip")
31+
res = session.run(
32+
"python",
33+
"-c",
34+
inspect.cleandoc("""
35+
import subprocess
36+
37+
subprocess.run(["pip", "install", "."])
38+
39+
import multipart
40+
"""),
41+
silent=True,
42+
)
43+
assert "FutureWarning: Please use `import python_multipart` instead." in res

pyproject.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,8 @@ path = "python_multipart/__init__.py"
7070
[tool.hatch.build.targets.sdist]
7171
include = ["/python_multipart", "/tests", "CHANGELOG.md", "LICENSE.txt", "_python_multipart.pth", "_python_multipart_loader.py"]
7272

73-
[tool.hatch.build.targets.wheel.force-include]
74-
"_python_multipart.pth" = "_python_multipart.pth"
75-
"_python_multipart_loader.py" = "_python_multipart_loader.py"
73+
[tool.hatch.build.targets.wheel]
74+
packages = ["python_multipart", "multipart"]
7675

7776
[tool.mypy]
7877
strict = true
@@ -91,6 +90,9 @@ skip-magic-trailing-comma = true
9190
combine-as-imports = true
9291
split-on-trailing-comma = false
9392

93+
[tool.ruff.lint.per-file-ignores]
94+
"multipart/*.py" = ["F403"]
95+
9496
[tool.coverage.run]
9597
branch = false
9698
omit = ["tests/*"]

scripts/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
* `scripts/test` - Run the test suite.
55
* `scripts/lint` - Run the code format.
66
* `scripts/check` - Run the lint in check mode, and the type checker.
7+
* `scripts/rename` - Check that the backward-compat `multipart` name works as expected.
78

89
Styled after GitHub's ["Scripts to Rule Them All"](https://github.com/github/scripts-to-rule-them-all).

0 commit comments

Comments
 (0)