Skip to content

Commit b4688c5

Browse files
committed
Python 2 backwards support with types
1 parent f18c8a7 commit b4688c5

File tree

3 files changed

+43
-40
lines changed

3 files changed

+43
-40
lines changed

tinify/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,23 @@ def from_url(self, url):
114114
if TYPE_CHECKING:
115115
# Help the type checker here, as we overrride the module with a singleton object.
116116
def get_client(): # type: () -> Client
117-
...
117+
pass
118118
key = None # type: Optional[str]
119119
app_identifier = None # type: Optional[str]
120120
proxy = None # type: Optional[str]
121121
compression_count = None # type: Optional[int]
122122

123123
def validate(): # type: () -> bool
124-
...
124+
pass
125125

126126
def from_file(path): # type: (str) -> Source
127-
...
127+
pass
128128

129129
def from_buffer(string): # type: (bytes) -> Source
130-
...
130+
pass
131131

132132
def from_url(url): # type: (str) -> Source
133-
...
133+
pass
134134

135135

136136
# Overwrite current module with singleton object.

tinify/source.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,17 @@
22
from __future__ import absolute_import, division, print_function, unicode_literals
33

44
import tinify
5-
from . import Result, ResultMeta
5+
import sys
6+
from tinify.result import Result
7+
from tinify.result_meta import ResultMeta
68

79
try:
8-
from typing import Union, Dict, IO, Any, TypedDict, List, Literal, Optional, Unpack, TYPE_CHECKING, overload
9-
10-
class ResizeOptions(TypedDict, total=False):
11-
method: Literal['scale', 'fit', 'cover', 'thumb']
12-
width: int
13-
height: int
14-
15-
ConvertTypes = Literal['image/webp', 'image/jpeg', 'image/png', "image/avif", "*/*"]
16-
class ConvertOptions(TypedDict, total=False):
17-
type: Union[ConvertTypes, List[ConvertTypes]]
18-
19-
class TransformOptions(TypedDict, total=False):
20-
background: str | Literal["white", "black"]
21-
22-
class S3StoreOptions(TypedDict, total=False):
23-
service: Literal['s3']
24-
aws_access_key_id: str
25-
aws_secret_access_key: str
26-
region: str
27-
path: str
28-
headers: Optional[Dict[str, str]]
29-
acl: Optional[Literal["no-acl"]]
30-
31-
class GCSStoreOptions(TypedDict, total=False):
32-
service: Literal['gcs']
33-
gcp_access_token: str
34-
path: str
35-
headers: Optional[Dict[str, str]]
36-
37-
PreserveOption = Literal['copyright', 'creation', 'location']
10+
from typing import Union, Dict, IO, Any, List, Literal, Optional, Unpack, TYPE_CHECKING, overload
11+
if sys.version_info.major > 3 and sys.version_info.minor > 8:
12+
from tinify.typed import *
3813
except ImportError:
3914
TYPE_CHECKING = False # type: ignore
4015

41-
42-
4316
class Source(object):
4417
@classmethod
4518
def from_file(cls, path): # type: (Union[str, IO]) -> Source
@@ -81,11 +54,11 @@ def transform(self, **options): # type: (Unpack[TransformOptions]) -> "Source"
8154
if TYPE_CHECKING:
8255
@overload
8356
def store(self, **options): # type: (Unpack[S3StoreOptions]) -> ResultMeta
84-
...
57+
pass
8558

8659
@overload
8760
def store(self, **options): # type: (Unpack[GCSStoreOptions]) -> ResultMeta
88-
...
61+
pass
8962

9063
def store(self, **options): # type: (Any) -> ResultMeta
9164
response = tinify.get_client().request('POST', self.url, self._merge_commands(store=options))

tinify/typed.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Union, Dict, List, Literal, Optional, TypedDict
2+
3+
class ResizeOptions(TypedDict,total=False):
4+
method: Literal['scale', 'fit', 'cover', 'thumb']
5+
width: int
6+
height: int
7+
8+
ConvertTypes = Literal['image/webp', 'image/jpeg', 'image/png', "image/avif", "*/*"]
9+
class ConvertOptions(TypedDict, total=False):
10+
type: Union[ConvertTypes, List[ConvertTypes]]
11+
12+
class TransformOptions(TypedDict, total=False):
13+
background: Union[str, Literal["white", "black"]]
14+
15+
class S3StoreOptions(TypedDict, total=False):
16+
service: Literal['s3']
17+
aws_access_key_id: str
18+
aws_secret_access_key: str
19+
region: str
20+
path: str
21+
headers: Optional[Dict[str, str]]
22+
acl: Optional[Literal["no-acl"]]
23+
24+
class GCSStoreOptions(TypedDict, total=False):
25+
service: Literal['gcs']
26+
gcp_access_token: str
27+
path: str
28+
headers: Optional[Dict[str, str]]
29+
30+
PreserveOption = Literal['copyright', 'creation', 'location']

0 commit comments

Comments
 (0)