Skip to content

Commit

Permalink
fix: use alternate scheme for importing multipart
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Oct 21, 2024
1 parent 72e30ea commit cda7cfc
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 51 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ jobs:
run: scripts/test

- name: Run rename test
run: uvx nox -s rename -P ${{ matrix.python-version }}

run: scripts/rename
# https://github.com/marketplace/actions/alls-green#why used for branch protection checks
check:
if: always()
Expand Down
1 change: 0 additions & 1 deletion _python_multipart.pth

This file was deleted.

37 changes: 0 additions & 37 deletions _python_multipart_loader.py

This file was deleted.

20 changes: 20 additions & 0 deletions multipart/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This only works if using a file system, other loaders not implemented.

import importlib.util
import sys
import warnings
from pathlib import Path

for p in sys.path:
file_path = Path(p, "multipart.py")
if file_path.is_file():
spec = importlib.util.spec_from_file_location("multipart", file_path)
assert spec is not None, f"{file_path} found but not loadable!"
module = importlib.util.module_from_spec(spec)
sys.modules["multipart"] = module
assert spec.loader is not None, f"{file_path} must be loadable!"
spec.loader.exec_module(module)
break
else:
warnings.warn("Please use `import python_multipart` instead.", FutureWarning, stacklevel=2)
from python_multipart import *
1 change: 1 addition & 0 deletions multipart/decoders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from python_multipart.decoders import *
1 change: 1 addition & 0 deletions multipart/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from python_multipart.exceptions import *
1 change: 1 addition & 0 deletions multipart/multipart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from python_multipart.multipart import *
28 changes: 21 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import inspect

import nox

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

ALL_PYTHONS = [
c.split()[-1]
for c in nox.project.load_toml("pyproject.toml")["project"]["classifiers"]
if c.startswith("Programming Language :: Python :: 3.")
]


@nox.session(python=ALL_PYTHONS)
@nox.session
def rename(session: nox.Session) -> None:
session.install(".")
assert "import python_multipart" in session.run("python", "-c", "import multipart", silent=True)
Expand All @@ -27,3 +23,21 @@ def rename(session: nox.Session) -> None:
assert "import python_multipart" not in session.run(
"python", "-c", "import python_multipart; python_multipart.parse_form", silent=True
)


@nox.session
def rename_inline(session: nox.Session) -> None:
session.install("pip")
res = session.run(
"python",
"-c",
inspect.cleandoc("""
import subprocess
subprocess.run(["pip", "install", "."])
import multipart
"""),
silent=True,
)
assert "FutureWarning: Please use `import python_multipart` instead." in res
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ path = "python_multipart/__init__.py"
[tool.hatch.build.targets.sdist]
include = ["/python_multipart", "/tests", "CHANGELOG.md", "LICENSE.txt", "_python_multipart.pth", "_python_multipart_loader.py"]

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

[tool.mypy]
strict = true
Expand All @@ -91,6 +90,9 @@ skip-magic-trailing-comma = true
combine-as-imports = true
split-on-trailing-comma = false

[tool.ruff.lint.per-file-ignores]
"multipart/*.py" = ["F403"]

[tool.coverage.run]
branch = false
omit = ["tests/*"]
Expand Down
1 change: 1 addition & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
* `scripts/test` - Run the test suite.
* `scripts/lint` - Run the code format.
* `scripts/check` - Run the lint in check mode, and the type checker.
* `scripts/rename` - Check that the backward-compat `multipart` name works as expected

Styled after GitHub's ["Scripts to Rule Them All"](https://github.com/github/scripts-to-rule-them-all).
2 changes: 1 addition & 1 deletion scripts/check
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -x

SOURCE_FILES="python_multipart tests"
SOURCE_FILES="python_multipart multipart tests"

uvx ruff format --check --diff $SOURCE_FILES
uvx ruff check $SOURCE_FILES
Expand Down
7 changes: 7 additions & 0 deletions scripts/rename
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh -e

set -x

uvx nox -s rename

uvx nox -s rename_inline

0 comments on commit cda7cfc

Please sign in to comment.