Skip to content

Commit 43d773e

Browse files
committed
refactor: update imports and tests for direct starlette/uvicorn dependencies
- Remove async extras from tox.ini since deps are now direct - Update _http/__init__.py to import StarletteApplication directly - Remove try/except ImportError for starlette in aio/__init__.py - Remove error message about installing with [async] extra - Update test_asgi.py to import starlette directly - Remove test_import_error_without_starlette from test_aio.py - Fix test_http.py monkeypatch to work with direct imports
1 parent 85bff31 commit 43d773e

File tree

6 files changed

+10
-48
lines changed

6 files changed

+10
-48
lines changed

src/functions_framework/_http/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from flask import Flask
1616

17+
from functions_framework._http.asgi import StarletteApplication
1718
from functions_framework._http.flask import FlaskApplication
1819

1920

@@ -35,17 +36,13 @@ def __init__(self, app, debug, **options):
3536
self.server_class = FlaskApplication
3637
else: # pragma: no cover
3738
if self.debug:
38-
from functions_framework._http.asgi import StarletteApplication
39-
4039
self.server_class = StarletteApplication
4140
else:
4241
try:
4342
from functions_framework._http.gunicorn import UvicornApplication
4443

4544
self.server_class = UvicornApplication
4645
except ImportError as e:
47-
from functions_framework._http.asgi import StarletteApplication
48-
4946
self.server_class = StarletteApplication
5047

5148
def run(self, host, port):

src/functions_framework/aio/__init__.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,12 @@
3636
MissingSourceException,
3737
)
3838

39-
try:
40-
from starlette.applications import Starlette
41-
from starlette.exceptions import HTTPException
42-
from starlette.middleware import Middleware
43-
from starlette.requests import Request
44-
from starlette.responses import JSONResponse, Response
45-
from starlette.routing import Route
46-
except ImportError:
47-
raise FunctionsFrameworkException(
48-
"Starlette is not installed. Install the framework with the 'async' extra: "
49-
"pip install functions-framework[async]"
50-
)
39+
from starlette.applications import Starlette
40+
from starlette.exceptions import HTTPException
41+
from starlette.middleware import Middleware
42+
from starlette.requests import Request
43+
from starlette.responses import JSONResponse, Response
44+
from starlette.routing import Route
5145

5246
HTTPResponse = Union[
5347
Response, # Functions can return a full Starlette Response object

tests/test_aio.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,6 @@
3535
TEST_FUNCTIONS_DIR = pathlib.Path(__file__).resolve().parent / "test_functions"
3636

3737

38-
def test_import_error_without_starlette(monkeypatch):
39-
import builtins
40-
41-
original_import = builtins.__import__
42-
43-
def mock_import(name, *args, **kwargs):
44-
if name.startswith("starlette"):
45-
raise ImportError(f"No module named '{name}'")
46-
return original_import(name, *args, **kwargs)
47-
48-
monkeypatch.setattr(builtins, "__import__", mock_import)
49-
50-
# Remove the module from sys.modules to force re-import
51-
if "functions_framework.aio" in sys.modules:
52-
del sys.modules["functions_framework.aio"]
53-
54-
with pytest.raises(exceptions.FunctionsFrameworkException) as excinfo:
55-
import functions_framework.aio
56-
57-
assert "Starlette is not installed" in str(excinfo.value)
58-
assert "pip install functions-framework[async]" in str(excinfo.value)
5938

6039

6140
def test_invalid_function_definition_missing_function_file():

tests/test_asgi.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919
import pytest
2020

2121
import functions_framework._http
22-
23-
try:
24-
from starlette.applications import Starlette
25-
except ImportError:
26-
pass
22+
from starlette.applications import Starlette
2723

2824

2925
def test_httpserver_detects_asgi_app():

tests/test_http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def test_httpserver_asgi(monkeypatch, debug, uvicorn_missing, expected):
154154
}
155155
options = {"a": pretend.stub(), "b": pretend.stub()}
156156

157-
from functions_framework._http import asgi
157+
import functions_framework._http
158158

159-
monkeypatch.setattr(asgi, "StarletteApplication", server_classes["starlette"])
159+
monkeypatch.setattr(functions_framework._http, "StarletteApplication", server_classes["starlette"])
160160

161161
if uvicorn_missing or platform.system() == "Windows":
162162
monkeypatch.setitem(sys.modules, "functions_framework._http.gunicorn", None)

tox.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ deps =
2929
pytest-cov
3030
pytest-integration
3131
pretend
32-
extras =
33-
async
3432
setenv =
3533
PYTESTARGS = --cov=functions_framework --cov-branch --cov-report term-missing --cov-fail-under=100
3634
# Python 3.7: Use .coveragerc-py37 to exclude aio module from coverage since it requires Python 3.8+ (Starlette dependency)
@@ -48,8 +46,6 @@ deps =
4846
isort
4947
mypy
5048
build
51-
extras =
52-
async
5349
commands =
5450
black --check src tests conftest.py --exclude tests/test_functions/background_load_error/main.py
5551
isort -c src tests conftest.py

0 commit comments

Comments
 (0)