Skip to content

Commit f18c8a7

Browse files
committed
Add type annotations
1 parent b8c1634 commit f18c8a7

20 files changed

+255
-70
lines changed

.github/workflows/ci-cd.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,34 @@ jobs:
4242
run: |
4343
pytest
4444
45+
Mypy:
46+
runs-on: ${{ matrix.os }}
47+
timeout-minutes: 10
48+
strategy:
49+
fail-fast: false
50+
matrix:
51+
python-version: [
52+
"3.12"
53+
]
54+
os: [ubuntu-latest, macOS-latest, windows-latest]
55+
steps:
56+
- uses: actions/checkout@v3
57+
- name: Set up Python ${{ matrix.python-version }}
58+
uses: actions/setup-python@v4
59+
with:
60+
python-version: ${{ matrix.python-version }}
61+
- name: Install dependencies
62+
run: |
63+
pip install -r test-requirements.txt -r requirements.txt
64+
- name: Run tests
65+
run: |
66+
mypy --check tinify
67+
4568
Integration_tests:
4669
if: github.event_name == 'push'
4770
runs-on: ${{ matrix.os }}
4871
timeout-minutes: 10
49-
needs: Unit_tests
72+
needs: [Unit_tests, Mypy, Unit_Tests_Py27]
5073
strategy:
5174
fail-fast: false
5275
matrix:

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## 1.6.1
1+
## 1.7.0
22

3+
* Added type annotations
34
* Updated runtime support
45
* Dropped python 3.7
56
* Added Python 3.12

setup.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
from version import __version__
1212

1313
install_require = ["requests >= 2.7.0, < 3.0.0"]
14-
tests_require = ["pytest", "pytest-xdist", "requests-mock"]
14+
tests_require = ["pytest", "pytest-xdist", "requests-mock", "types-requests"]
15+
16+
if sys.version_info.major > 2:
17+
tests_require.append("mypy")
1518

1619
setup(
1720
name="tinify",
@@ -26,7 +29,7 @@
2629
packages=["tinify"],
2730
package_data={
2831
"": ["LICENSE", "README.md"],
29-
"tinify": ["data/cacert.pem"],
32+
"tinify": ["data/cacert.pem", "py.typed"],
3033
},
3134
install_requires=install_require,
3235
tests_require=tests_require,

test/__init__.py

Whitespace-only changes.

test/integration.py

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
if not os.environ.get("TINIFY_KEY"):
99
sys.exit("Set the TINIFY_KEY environment variable.")
1010

11+
try:
12+
from typing import TYPE_CHECKING
13+
if TYPE_CHECKING:
14+
from tinify.source import Source
15+
except ImportError:
16+
pass
17+
1118

1219
@contextmanager
1320
def create_named_tmpfile():
@@ -22,20 +29,26 @@ def create_named_tmpfile():
2229
finally:
2330
os.unlink(tmp.name)
2431

32+
@pytest.fixture(scope="module", autouse=True)
33+
def tinify_patch():
34+
tinify.key = os.environ.get("TINIFY_KEY")
35+
tinify.proxy = os.environ.get("TINIFY_PROXY")
36+
37+
yield
38+
39+
tinify.key = None
40+
tinify.proxy = None
2541

2642
# Fixture for shared resources
2743
@pytest.fixture(scope="module")
2844
def optimized_image():
29-
tinify.key = os.environ.get("TINIFY_KEY")
30-
tinify.proxy = os.environ.get("TINIFY_PROXY")
31-
3245
unoptimized_path = os.path.join(
3346
os.path.dirname(__file__), "examples", "voormedia.png"
3447
)
3548
return tinify.from_file(unoptimized_path)
3649

3750

38-
def test_should_compress_from_file(optimized_image):
51+
def test_should_compress_from_file(optimized_image): # type: (Source) -> None
3952
with create_named_tmpfile() as tmp:
4053
optimized_image.to_file(tmp)
4154

@@ -69,10 +82,9 @@ def test_should_compress_from_url():
6982
assert b"Copyright Voormedia" not in contents
7083

7184

72-
def test_should_resize(optimized_image):
85+
def test_should_resize(optimized_image): # type: (Source) -> None
7386
with create_named_tmpfile() as tmp:
7487
optimized_image.resize(method="fit", width=50, height=20).to_file(tmp)
75-
7688
size = os.path.getsize(tmp)
7789
with open(tmp, "rb") as f:
7890
contents = f.read()
@@ -84,7 +96,7 @@ def test_should_resize(optimized_image):
8496
assert b"Copyright Voormedia" not in contents
8597

8698

87-
def test_should_preserve_metadata(optimized_image):
99+
def test_should_preserve_metadata(optimized_image): # type: (Source) -> None
88100
with create_named_tmpfile() as tmp:
89101
optimized_image.preserve("copyright", "creation").to_file(tmp)
90102

@@ -99,11 +111,30 @@ def test_should_preserve_metadata(optimized_image):
99111
assert b"Copyright Voormedia" in contents
100112

101113

102-
def test_should_transcode_image(optimized_image):
114+
def test_should_transcode_image(optimized_image): # type: (Source) -> None
103115
with create_named_tmpfile() as tmp:
104-
optimized_image.convert(type=["image/webp"]).to_file(tmp)
116+
conv = optimized_image.convert(type=["image/webp"])
117+
conv.to_file(tmp)
105118
with open(tmp, "rb") as f:
106119
content = f.read()
107120

108121
assert b"RIFF" == content[:4]
109122
assert b"WEBP" == content[8:12]
123+
124+
assert conv.result().size < optimized_image.result().size
125+
assert conv.result().media_type == "image/webp"
126+
assert conv.result().extension == "webp"
127+
128+
129+
def test_should_handle_invalid_key():
130+
invalid_key = "invalid_key"
131+
tinify.key = invalid_key
132+
with pytest.raises(tinify.AccountError):
133+
tinify.from_url(
134+
"https://raw.githubusercontent.com/tinify/tinify-python/master/test/examples/voormedia.png"
135+
)
136+
tinify.key = os.environ.get("TINIFY_KEY")
137+
138+
def test_should_handle_invalid_image():
139+
with pytest.raises(tinify.ClientError):
140+
tinify.from_buffer("invalid_image.png")

test/unit/__init__.py

Whitespace-only changes.

test/conftest.py renamed to test/unit/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
@pytest.fixture
88
def dummy_file():
9-
return os.path.join(os.path.dirname(__file__), "examples", "dummy.png")
9+
return os.path.join(os.path.dirname(__file__), "..", "examples", "dummy.png")
1010

1111

1212
@pytest.fixture(autouse=True)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)