Skip to content

Commit

Permalink
♻️ Remove compatibility with Python 3.7 (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
perdy committed Aug 7, 2023
1 parent 38afcbd commit 6c7f4b2
Show file tree
Hide file tree
Showing 20 changed files with 138 additions and 498 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_and_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [ "3.7", "3.8", "3.9", "3.10", "3.11"]
python: [ "3.8", "3.9", "3.10", "3.11"]
container:
image: python:${{ matrix.python }}
steps:
Expand Down Expand Up @@ -100,7 +100,7 @@ jobs:
strategy:
matrix:
linux: ["slim"]
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python: ["3.8", "3.9", "3.10", "3.11"]
schemas: ["pydantic", "marshmallow", "typesystem"]
steps:
- name: Check out the repo
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_pull_request_branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python: ["3.8", "3.9", "3.10", "3.11"]
container:
image: python:${{ matrix.python }}
steps:
Expand Down
6 changes: 0 additions & 6 deletions flama/schemas/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
from flama import schemas, types
from flama.injection.resolver import Parameter as InjectionParameter

if sys.version_info < (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
from typing_extensions import get_args, get_origin

t.get_args = get_args
t.get_origin = get_origin

if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import TypeGuard

Expand Down
11 changes: 1 addition & 10 deletions flama/serialize/serializers/pytorch.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import codecs
import importlib.metadata
import io
import sys
import typing as t

from flama.serialize.base import Serializer
from flama.serialize.types import Framework

if sys.version_info < (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
import importlib

import importlib_metadata

importlib.metadata = importlib_metadata
else:
import importlib.metadata

try:
import torch
except Exception: # pragma: no cover
Expand Down
11 changes: 1 addition & 10 deletions flama/serialize/serializers/sklearn.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
import codecs
import importlib.metadata
import logging
import math
import pickle
import sys
import typing as t
import warnings

from flama import types
from flama.serialize.base import Serializer
from flama.serialize.types import Framework

if sys.version_info < (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
import importlib

import importlib_metadata

importlib.metadata = importlib_metadata
else:
import importlib.metadata

logger = logging.getLogger(__name__)


Expand Down
11 changes: 1 addition & 10 deletions flama/serialize/serializers/tensorflow.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
import codecs
import importlib.metadata
import io
import json
import sys
import tarfile
import typing as t
from tempfile import TemporaryDirectory

from flama.serialize.base import Serializer
from flama.serialize.types import Framework

if sys.version_info < (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
import importlib

import importlib_metadata

importlib.metadata = importlib_metadata
else:
import importlib.metadata

try:
import tensorflow as tf
except Exception: # pragma: no cover
Expand Down
5 changes: 0 additions & 5 deletions flama/types/asgi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import sys
import typing as t

if sys.version_info < (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
from typing_extensions import Protocol

t.Protocol = Protocol

if sys.version_info < (3, 10): # PORT: Remove when stop supporting 3.9 # pragma: no cover
from typing_extensions import Concatenate, ParamSpec

Expand Down
402 changes: 115 additions & 287 deletions poetry.lock

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ include = ["flama/py.typed", "flama/templates/**/*"]
exclude = []

[tool.poetry.dependencies]
python = ">=3.7,<3.12"
python = ">=3.8,<3.12"
starlette = ">=0.21.0,<1.0.0"
click = "^8.1"
uvicorn = "^0.22"
Jinja2 = "^3.1.2"
typing-extensions = { version = "^4.4", python = "<3.10" }
importlib-metadata = { version = "^4.2", python = "<3.8" }
pydantic = { version = "^2.0", optional = true }
marshmallow = { version = "^3.0", optional = true }
apispec = { version = "^6.0", optional = true }
Expand All @@ -64,10 +63,7 @@ pytest = "^7.0"
pytest-xdist = { version = "^3.0", extras = ["psutil"] }
pytest-cov = "^4.0"
pytest-asyncio = "^0.20"
ipython = [
{ version = "^7.0", python = ">=3.7,<3.8" },
{ version = "^8.0", python = ">=3.8" }
]
ipython = "^8.0"
isort = "^5.10"
ruff = "^0.0.231"
black = { version = "^22.3", extras = ["d"] }
Expand All @@ -80,7 +76,6 @@ requests = "^2.22"
Faker = "^8.2"
SQLAlchemy = { version = "^2.0", extras = ["asyncio"] }
gevent = "^22.10"
asyncmock = { version = "^0.4", python = ">=3.7,<3.8" }
pyright = "^1.1.320"

[tool.poetry.group.schemas]
Expand All @@ -96,9 +91,9 @@ typesystem = "^0.4"
optional = true

[tool.poetry.group.ml.dependencies]
scikit-learn = { version = "^1.3.0", python = ">=3.8" }
tensorflow-cpu = { version = "^2.12.0", python = ">=3.8", platform = "linux"}
torch = { version = "^2.0.0", python = ">=3.8" }
scikit-learn = "^1.3.0"
tensorflow-cpu = { version = "^2.12.0", platform = "linux"}
torch = "^2.0.0"

[tool.black]
line-length = 120
Expand Down
2 changes: 1 addition & 1 deletion scripts/docker_push
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

LINUX_VERSIONS=("slim")
PYTHON_VERSIONS=("3.7" "3.8" "3.9" "3.10" "3.11")
PYTHON_VERSIONS=("3.8" "3.9" "3.10" "3.11")
SCHEMA_LIBS=("pydantic" "marshmallow" "typesystem")

DEFAULT_LINUX="slim"
Expand Down
7 changes: 1 addition & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import asyncio
import sys
import tempfile
import typing as t
from contextlib import ExitStack
from pathlib import Path
from unittest.mock import AsyncMock

import marshmallow
import pydantic
Expand All @@ -18,11 +18,6 @@
from flama.testclient import TestClient
from tests.utils import ExceptionContext, installed

if sys.version_info >= (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
from unittest.mock import AsyncMock
else: # pragma: no cover
from asyncmock import AsyncMock

try:
import numpy as np
except Exception:
Expand Down
16 changes: 3 additions & 13 deletions tests/debug/test_data_structures.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import dataclasses
import sys
from unittest.mock import MagicMock, patch

import pytest
Expand Down Expand Up @@ -32,9 +31,6 @@ def test_from_request(self, asgi_scope, asgi_receive, asgi_send):


class TestCaseError:
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
def test_from_exception(self):
try:
raise ValueError("Foo")
Expand All @@ -50,16 +46,13 @@ def test_from_exception(self):
assert frame == {
"filename": "tests/debug/test_data_structures.py",
"function": "test_from_exception",
"line": 40,
"line": 36,
"vendor": None,
}
assert result == {"description": "Foo", "error": "ValueError"}


class TestCaseEnvironment:
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
def test_from_system(self):
result = dataclasses.asdict(Environment.from_system())

Expand All @@ -69,9 +62,6 @@ def test_from_system(self):


class TestCaseApp:
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
def test_from_app(self):
def foo_handler():
...
Expand Down Expand Up @@ -99,7 +89,7 @@ def bar_handler():
"endpoint": "foo_handler",
"module": "tests.debug.test_data_structures",
"file": "tests/debug/test_data_structures.py",
"line": 76,
"line": 66,
"name": "foo",
},
{
Expand All @@ -110,7 +100,7 @@ def bar_handler():
"path": "/",
"module": "tests.debug.test_data_structures",
"file": "tests/debug/test_data_structures.py",
"line": 79,
"line": 69,
"endpoint": "bar_handler",
"name": "bar",
}
Expand Down
38 changes: 1 addition & 37 deletions tests/debug/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
from unittest.mock import MagicMock, call, patch
from unittest.mock import AsyncMock, MagicMock, call, patch

import pytest
import starlette.exceptions
Expand All @@ -9,11 +8,6 @@
from flama.debug.data_structures import ErrorContext
from flama.debug.middleware import BaseErrorMiddleware, ExceptionMiddleware, ServerErrorMiddleware

if sys.version_info >= (3, 8): # PORT: Remove when stop supporting 3.7 # pragma: no cover
from unittest.mock import AsyncMock
else: # pragma: no cover
from asyncmock import AsyncMock


class TestCaseBaseErrorMiddleware:
@pytest.fixture
Expand All @@ -37,9 +31,6 @@ def test_init(self, middleware_cls):
assert middleware.app == app
assert middleware.debug

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_call_http(self, middleware_cls, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "http"
exc = ValueError()
Expand All @@ -55,9 +46,6 @@ async def test_call_http(self, middleware_cls, asgi_scope, asgi_receive, asgi_se
call(asgi_scope, asgi_receive, asgi_send, exc, False)
]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_call_websocket(self, middleware_cls, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "websocket"
exc = ValueError()
Expand Down Expand Up @@ -250,9 +238,6 @@ def test_get_handler(self, middleware, handler, status_code, exc_class, key, exc
),
indirect=["exception"],
)
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_process_exception(
self, middleware, asgi_scope, asgi_receive, asgi_send, request_type, response_started, exception
):
Expand Down Expand Up @@ -319,9 +304,6 @@ async def test_process_exception(
),
),
)
@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
def test_http_exception_handler(
self, middleware, asgi_scope, asgi_receive, asgi_send, debug, accept, exc, response_class, response_params
):
Expand All @@ -343,9 +325,6 @@ def test_http_exception_handler(
assert isinstance(response, response_class)
assert response_mock.call_args_list == [call(**response_params)]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_websocket_exception_handler(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "websocket"
exc = exceptions.WebSocketException(1011, "Foo reason")
Expand All @@ -355,9 +334,6 @@ async def test_websocket_exception_handler(self, middleware, asgi_scope, asgi_re

assert websocket_close_mock.call_args_list == [call(code=exc.code, reason=exc.reason)]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_not_found_handler_http(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "http"
asgi_scope["app"] = MagicMock(Flama)
Expand All @@ -370,9 +346,6 @@ async def test_not_found_handler_http(self, middleware, asgi_scope, asgi_receive
call(asgi_scope, asgi_receive, asgi_send, exc=exceptions.HTTPException(status_code=404))
]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_not_found_handler_http_no_app(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "http"
exc = exceptions.NotFoundException()
Expand All @@ -382,9 +355,6 @@ async def test_not_found_handler_http_no_app(self, middleware, asgi_scope, asgi_

assert mock.call_args_list == [call("Not Found", status_code=404)]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_not_found_handler_websocket(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "websocket"
exc = exceptions.NotFoundException()
Expand All @@ -396,9 +366,6 @@ async def test_not_found_handler_websocket(self, middleware, asgi_scope, asgi_re
call(asgi_scope, asgi_receive, asgi_send, exc=exceptions.WebSocketException(1000))
]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_method_not_allowed_handler_http(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "http"
asgi_scope["app"] = MagicMock(Flama)
Expand All @@ -416,9 +383,6 @@ async def test_method_not_allowed_handler_http(self, middleware, asgi_scope, asg
)
]

@pytest.mark.skipif(
sys.version_info < (3, 8), reason="requires python3.8 or higher to use async mocks"
) # PORT: Remove when stop supporting 3.7
async def test_method_not_allowed_handler_websocket(self, middleware, asgi_scope, asgi_receive, asgi_send):
asgi_scope["type"] = "websocket"
exc = exceptions.MethodNotAllowedException("/", "POST", {"GET"})
Expand Down
Loading

0 comments on commit 6c7f4b2

Please sign in to comment.