Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Python 2 backwards support with types
  • Loading branch information
rkoopmans committed Mar 18, 2025
commit b4688c517549b14f9986ca8641dfa261754e9866
10 changes: 5 additions & 5 deletions tinify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,23 @@ def from_url(self, url):
if TYPE_CHECKING:
# Help the type checker here, as we overrride the module with a singleton object.
def get_client(): # type: () -> Client
...
pass
key = None # type: Optional[str]
app_identifier = None # type: Optional[str]
proxy = None # type: Optional[str]
compression_count = None # type: Optional[int]

def validate(): # type: () -> bool
...
pass

def from_file(path): # type: (str) -> Source
...
pass

def from_buffer(string): # type: (bytes) -> Source
...
pass

def from_url(url): # type: (str) -> Source
...
pass


# Overwrite current module with singleton object.
Expand Down
43 changes: 8 additions & 35 deletions tinify/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,17 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import tinify
from . import Result, ResultMeta
import sys
from tinify.result import Result
from tinify.result_meta import ResultMeta

try:
from typing import Union, Dict, IO, Any, TypedDict, List, Literal, Optional, Unpack, TYPE_CHECKING, overload

class ResizeOptions(TypedDict, total=False):
method: Literal['scale', 'fit', 'cover', 'thumb']
width: int
height: int

ConvertTypes = Literal['image/webp', 'image/jpeg', 'image/png', "image/avif", "*/*"]
class ConvertOptions(TypedDict, total=False):
type: Union[ConvertTypes, List[ConvertTypes]]

class TransformOptions(TypedDict, total=False):
background: str | Literal["white", "black"]

class S3StoreOptions(TypedDict, total=False):
service: Literal['s3']
aws_access_key_id: str
aws_secret_access_key: str
region: str
path: str
headers: Optional[Dict[str, str]]
acl: Optional[Literal["no-acl"]]

class GCSStoreOptions(TypedDict, total=False):
service: Literal['gcs']
gcp_access_token: str
path: str
headers: Optional[Dict[str, str]]

PreserveOption = Literal['copyright', 'creation', 'location']
from typing import Union, Dict, IO, Any, List, Literal, Optional, Unpack, TYPE_CHECKING, overload
if sys.version_info.major > 3 and sys.version_info.minor > 8:
from tinify.typed import *
except ImportError:
TYPE_CHECKING = False # type: ignore



class Source(object):
@classmethod
def from_file(cls, path): # type: (Union[str, IO]) -> Source
Expand Down Expand Up @@ -81,11 +54,11 @@ def transform(self, **options): # type: (Unpack[TransformOptions]) -> "Source"
if TYPE_CHECKING:
@overload
def store(self, **options): # type: (Unpack[S3StoreOptions]) -> ResultMeta
...
pass

@overload
def store(self, **options): # type: (Unpack[GCSStoreOptions]) -> ResultMeta
...
pass

def store(self, **options): # type: (Any) -> ResultMeta
response = tinify.get_client().request('POST', self.url, self._merge_commands(store=options))
Expand Down
30 changes: 30 additions & 0 deletions tinify/typed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Union, Dict, List, Literal, Optional, TypedDict

class ResizeOptions(TypedDict,total=False):
method: Literal['scale', 'fit', 'cover', 'thumb']
width: int
height: int

ConvertTypes = Literal['image/webp', 'image/jpeg', 'image/png', "image/avif", "*/*"]
class ConvertOptions(TypedDict, total=False):
type: Union[ConvertTypes, List[ConvertTypes]]

class TransformOptions(TypedDict, total=False):
background: Union[str, Literal["white", "black"]]

class S3StoreOptions(TypedDict, total=False):
service: Literal['s3']
aws_access_key_id: str
aws_secret_access_key: str
region: str
path: str
headers: Optional[Dict[str, str]]
acl: Optional[Literal["no-acl"]]

class GCSStoreOptions(TypedDict, total=False):
service: Literal['gcs']
gcp_access_token: str
path: str
headers: Optional[Dict[str, str]]

PreserveOption = Literal['copyright', 'creation', 'location']
Loading