def ensure_multipart_is_installed() -> None:
try:
- # __version__ is available in both multiparts, and can be mocked
- from multipart import __version__
+ from python_multipart import __version__
- assert __version__
+ # Import an attribute that can be mocked/deleted in testing
+ assert __version__ > "0.0.12"
+ except (ImportError, AssertionError):
try:
- # parse_options_header is only available in the right multipart
- from multipart.multipart import parse_options_header
+ # __version__ is available in both multiparts, and can be mocked
+ from multipart import __version__ # type: ignore[no-redef,import-untyped]
- assert parse_options_header # type: ignore[truthy-function]
+ assert __version__
+ try:
+ # parse_options_header is only available in the right multipart
+ from multipart.multipart import ( # type: ignore[import-untyped]
+ parse_options_header,
+ )
+
+ assert parse_options_header
+ except ImportError:
+ logger.error(multipart_incorrect_install_error)
+ raise RuntimeError(multipart_incorrect_install_error) from None
except ImportError:
- logger.error(multipart_incorrect_install_error)
- raise RuntimeError(multipart_incorrect_install_error) from None
- except ImportError:
- logger.error(multipart_not_installed_error)
- raise RuntimeError(multipart_not_installed_error) from None
+ logger.error(multipart_not_installed_error)
+ raise RuntimeError(multipart_not_installed_error) from None
def get_param_sub_dependant(
+import warnings
+
import pytest
from fastapi import FastAPI, File, Form, UploadFile
from fastapi.dependencies.utils import (
def test_incorrect_multipart_installed_form(monkeypatch):
- monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
app = FastAPI()
def test_incorrect_multipart_installed_file_upload(monkeypatch):
- monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
app = FastAPI()
def test_incorrect_multipart_installed_file_bytes(monkeypatch):
- monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
app = FastAPI()
def test_incorrect_multipart_installed_multi_form(monkeypatch):
- monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
app = FastAPI()
def test_incorrect_multipart_installed_form_file(monkeypatch):
- monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.multipart.parse_options_header", raising=False)
with pytest.raises(RuntimeError, match=multipart_incorrect_install_error):
app = FastAPI()
def test_no_multipart_installed(monkeypatch):
- monkeypatch.delattr("multipart.__version__", raising=False)
- with pytest.raises(RuntimeError, match=multipart_not_installed_error):
- app = FastAPI()
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.__version__", raising=False)
+ with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ app = FastAPI()
- @app.post("/")
- async def root(username: str = Form()):
- return username # pragma: nocover
+ @app.post("/")
+ async def root(username: str = Form()):
+ return username # pragma: nocover
def test_no_multipart_installed_file(monkeypatch):
- monkeypatch.delattr("multipart.__version__", raising=False)
- with pytest.raises(RuntimeError, match=multipart_not_installed_error):
- app = FastAPI()
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.__version__", raising=False)
+ with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ app = FastAPI()
- @app.post("/")
- async def root(f: UploadFile = File()):
- return f # pragma: nocover
+ @app.post("/")
+ async def root(f: UploadFile = File()):
+ return f # pragma: nocover
def test_no_multipart_installed_file_bytes(monkeypatch):
- monkeypatch.delattr("multipart.__version__", raising=False)
- with pytest.raises(RuntimeError, match=multipart_not_installed_error):
- app = FastAPI()
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.__version__", raising=False)
+ with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ app = FastAPI()
- @app.post("/")
- async def root(f: bytes = File()):
- return f # pragma: nocover
+ @app.post("/")
+ async def root(f: bytes = File()):
+ return f # pragma: nocover
def test_no_multipart_installed_multi_form(monkeypatch):
- monkeypatch.delattr("multipart.__version__", raising=False)
- with pytest.raises(RuntimeError, match=multipart_not_installed_error):
- app = FastAPI()
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.__version__", raising=False)
+ with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ app = FastAPI()
- @app.post("/")
- async def root(username: str = Form(), password: str = Form()):
- return username # pragma: nocover
+ @app.post("/")
+ async def root(username: str = Form(), password: str = Form()):
+ return username # pragma: nocover
def test_no_multipart_installed_form_file(monkeypatch):
- monkeypatch.delattr("multipart.__version__", raising=False)
- with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
+ monkeypatch.delattr("multipart.__version__", raising=False)
+ with pytest.raises(RuntimeError, match=multipart_not_installed_error):
+ app = FastAPI()
+
+ @app.post("/")
+ async def root(username: str = Form(), f: UploadFile = File()):
+ return username # pragma: nocover
+
+
+def test_old_multipart_installed(monkeypatch):
+ monkeypatch.setattr("python_multipart.__version__", "0.0.12")
+ with warnings.catch_warnings(record=True):
+ warnings.simplefilter("always")
app = FastAPI()
@app.post("/")
- async def root(username: str = Form(), f: UploadFile = File()):
+ async def root(username: str = Form()):
return username # pragma: nocover