Skip to content

Commit

Permalink
More python 3.7 tidying (#2063)
Browse files Browse the repository at this point in the history
* remove checks for python < 3.8

We no longer support python < 3.8

* update typing imports for python >= 3.8

We can rely on the following being present now: get_args, get_origin, Literal,
TypedDict. I also added comments indicating when various other typing_extensions
features landed in stdlib typing.

* make ruff happy
  • Loading branch information
philandstuff authored Nov 19, 2024
1 parent 746ec53 commit 425d5a2
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 56 deletions.
11 changes: 1 addition & 10 deletions python/cog/command/ast_openapi_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ def get_value(node: ast.AST) -> "AstVal":
"""Return the value of constant or list of constants"""
if isinstance(node, ast.Constant):
return node.value
# DeprecationWarning: ast.Str | ast.Num is deprecated and will be removed in Python 3.14; use ast.Constant instead
if sys.version_info < (3, 8):
if isinstance(node, (ast.Str, ast.Bytes)):
return node.s
if isinstance(node, ast.Num):
return node.n
if isinstance(node, (ast.List, ast.Tuple)):
return [get_value(e) for e in node.elts]
if isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
Expand Down Expand Up @@ -536,10 +530,7 @@ def extract_info(code: str) -> "JSONDict": # pylint: disable=too-many-branches,
msg = "unknown argument for Input"
raise ValueError(msg)
kws[kw.arg] = to_serializable(get_value(kw.value))
elif isinstance(default, (ast.Constant, ast.List, ast.Tuple)) or (
# DeprecationWarning: ast.Str | ast.Num is deprecated and will be removed in Python 3.14; use ast.Constant instead
sys.version_info < (3, 8) and isinstance(default, (ast.Str, ast.Num))
):
elif isinstance(default, (ast.Constant, ast.List, ast.Tuple)):
kws = {"default": to_serializable(get_value(default))} # could be None
elif default == ...: # no default
kws = {}
Expand Down
20 changes: 4 additions & 16 deletions python/cog/env_property.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import os
from functools import wraps
from typing import Any, Callable, Optional, TypeVar, Union
from typing import Any, Callable, Optional, TypeVar, Union, get_args, get_origin

R = TypeVar("R")


def _get_origin(typ: Any) -> Any:
if hasattr(typ, "__origin__"):
return typ.__origin__
return None


def _get_args(typ: Any) -> Any:
if hasattr(typ, "__args__"):
return typ.__args__
return ()


def env_property(
env_var: str,
) -> Callable[[Callable[[Any], R]], Callable[[Any], R]]:
Expand All @@ -29,10 +17,10 @@ def wrapper(*args: Any, **kwargs: Any) -> R:
if result is not None:
expected_type = func.__annotations__.get("return", str)
if (
_get_origin(expected_type) is Optional
or _get_origin(expected_type) is Union
get_origin(expected_type) is Optional
or get_origin(expected_type) is Union
):
expected_type = _get_args(expected_type)[0]
expected_type = get_args(expected_type)[0]
return expected_type(result)
result = func(*args, **kwargs)
return result
Expand Down
10 changes: 3 additions & 7 deletions python/cog/predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,14 @@
Callable,
Dict,
List,
Literal,
Optional,
Type,
Union,
cast,
get_args,
get_origin,
)

try:
from typing import Literal, get_args, get_origin
except ImportError: # Python < 3.8
from typing_compat import get_args, get_origin # type: ignore
from typing_extensions import Literal

from unittest.mock import patch

import pydantic
Expand Down
2 changes: 1 addition & 1 deletion python/cog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Config:
if PYDANTIC_V2:
from pydantic.networks import UrlConstraints
from pydantic_core import Url
from typing_extensions import Annotated
from typing_extensions import Annotated # added to typing in python 3.9

WebhookUrl = Annotated[
Url, UrlConstraints(allowed_schemes=["http", "https"], max_length=65536)
Expand Down
2 changes: 1 addition & 1 deletion python/cog/server/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from typing import Any, BinaryIO, Callable, Dict, List, Sequence, TextIO, Union

import pydantic
from typing_extensions import Self
from typing_extensions import Self # added to typing in python 3.11

from ..types import PYDANTIC_V2
from .errors import CogRuntimeError, CogTimeoutError
Expand Down
5 changes: 1 addition & 4 deletions python/cog/server/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from contextlib import contextmanager
from contextvars import ContextVar
from typing import Generator, Optional

# TypedDict was added in 3.8
from typing_extensions import TypedDict
from typing import Generator, Optional, TypedDict


# See: https://www.w3.org/TR/trace-context/
Expand Down
13 changes: 2 additions & 11 deletions python/cog/server/useragent.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import sys
from importlib.metadata import version


def _get_version() -> str:
try:
if sys.version_info >= (3, 8):
from importlib.metadata import (
version, # pylint: disable=import-outside-toplevel
)

return version("cog")
else:
import pkg_resources # pylint: disable=import-outside-toplevel

return pkg_resources.get_distribution("cog").version
return version("cog")
except Exception: # pylint: disable=broad-exception-caught
return "unknown"

Expand Down
3 changes: 2 additions & 1 deletion python/cog/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
List,
Optional,
Type,
TypedDict,
TypeVar,
Union,
)

import pydantic
import requests
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired # added to typing in python 3.11

if pydantic.__version__.startswith("1."):
PYDANTIC_V2 = False
Expand Down
2 changes: 1 addition & 1 deletion python/tests/server/test_http_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def test_choices_str(client):


@uses_predictor("input_choices_iterable")
def test_choices_str(client):
def test_choices_str_iterable(client):
resp = client.post("/predictions", json={"input": {"text": "foo"}})
assert resp.status_code == 200
resp = client.post("/predictions", json={"input": {"text": "baz"}})
Expand Down
2 changes: 1 addition & 1 deletion python/tests/server/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import os
import threading
import time
from typing import TYPE_CHECKING, Any, Dict, List, Optional
import uuid
from typing import TYPE_CHECKING, Any, Dict, List, Optional

import pytest
from attrs import define, evolve, field, frozen
Expand Down
2 changes: 0 additions & 2 deletions test-integration/test_integration/test_predict.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
import pathlib
import shutil
import subprocess
import sys
from pathlib import Path

import httpx
Expand Down
1 change: 0 additions & 1 deletion test-integration/test_integration/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import time

import httpx

from packaging.version import VERSION_PATTERN

# From the SemVer spec: https://semver.org/
Expand Down

0 comments on commit 425d5a2

Please sign in to comment.