Skip to content

Commit 446d7a2

Browse files
committed
made mypy happy-ish
1 parent fdfd9cf commit 446d7a2

4 files changed

Lines changed: 67 additions & 107 deletions

File tree

‎portalocker/portalocker.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def unlock(file_: typing.IO):
102102
# but any callable that matches the syntax will be accepted.
103103
LOCKER = fcntl.flock # pyright: ignore[reportConstantRedefinition]
104104

105-
def lock(file: int | types.IO, flags: LockFlags):
105+
def lock(file: int | types.IO, flags: LockFlags): # type: ignore[misc]
106106
assert LOCKER is not None, 'We need a locking function in `LOCKER` '
107107
# Locking with NON_BLOCKING without EXCLUSIVE or SHARED enabled
108108
# results in an error
@@ -146,7 +146,7 @@ def lock(file: int | types.IO, flags: LockFlags):
146146
fh=file,
147147
) from exc_value
148148

149-
def unlock(file: types.IO):
149+
def unlock(file: types.IO): # type: ignore[misc]
150150
assert LOCKER is not None, 'We need a locking function in `LOCKER` '
151151
LOCKER(file.fileno(), LockFlags.UNBLOCK)
152152

‎portalocker/types.py‎

Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,50 @@
44
import typing
55
from typing import Union
66

7+
# fmt: off
78
Mode = typing.Literal[
89
# Text modes
9-
'r',
10-
'rt',
11-
'tr', # Read text
12-
'w',
13-
'wt',
14-
'tw', # Write text
15-
'a',
16-
'at',
17-
'ta', # Append text
18-
'x',
19-
'xt',
20-
'tx', # Exclusive creation text
21-
'r+',
22-
'+r',
23-
'rt+',
24-
'r+t',
25-
'+rt',
26-
'tr+',
27-
't+r',
28-
'+tr', # Read and write text
29-
'w+',
30-
'+w',
31-
'wt+',
32-
'w+t',
33-
'+wt',
34-
'tw+',
35-
't+w',
36-
'+tw', # Write and read text
37-
'a+',
38-
'+a',
39-
'at+',
40-
'a+t',
41-
'+at',
42-
'ta+',
43-
't+a',
44-
'+ta', # Append and read text
45-
'x+',
46-
'+x',
47-
'xt+',
48-
'x+t',
49-
'+xt',
50-
'tx+',
51-
't+x',
52-
'+tx', # Exclusive creation and read text
53-
'U',
54-
'rU',
55-
'Ur',
56-
'rtU',
57-
'rUt',
58-
'Urt',
59-
'trU',
60-
'tUr',
61-
'Utr', # Universal newline support
10+
# Read text
11+
'r', 'rt', 'tr',
12+
# Write text
13+
'w', 'wt', 'tw',
14+
# Append text
15+
'a', 'at', 'ta',
16+
# Exclusive creation text
17+
'x', 'xt', 'tx',
18+
# Read and write text
19+
'r+', '+r', 'rt+', 'r+t', '+rt', 'tr+', 't+r', '+tr',
20+
# Write and read text
21+
'w+', '+w', 'wt+', 'w+t', '+wt', 'tw+', 't+w', '+tw',
22+
# Append and read text
23+
'a+', '+a', 'at+', 'a+t', '+at', 'ta+', 't+a', '+ta',
24+
# Exclusive creation and read text
25+
'x+', '+x', 'xt+', 'x+t', '+xt', 'tx+', 't+x', '+tx',
26+
# Universal newline support
27+
'U', 'rU', 'Ur', 'rtU', 'rUt', 'Urt', 'trU', 'tUr', 'Utr',
28+
6229
# Binary modes
63-
'rb',
64-
'br', # Read binary
65-
'wb',
66-
'bw', # Write binary
67-
'ab',
68-
'ba', # Append binary
69-
'xb',
70-
'bx', # Exclusive creation binary
71-
'rb+',
72-
'r+b',
73-
'+rb',
74-
'br+',
75-
'b+r',
76-
'+br', # Read and write binary
77-
'wb+',
78-
'w+b',
79-
'+wb',
80-
'bw+',
81-
'b+w',
82-
'+bw', # Write and read binary
83-
'ab+',
84-
'a+b',
85-
'+ab',
86-
'ba+',
87-
'b+a',
88-
'+ba', # Append and read binary
89-
'xb+',
90-
'x+b',
91-
'+xb',
92-
'bx+',
93-
'b+x',
94-
'+bx', # Exclusive creation and read binary
95-
'rbU',
96-
'rUb',
97-
'Urb',
98-
'brU',
99-
'bUr',
100-
'Ubr',
30+
# Read binary
31+
'rb', 'br',
32+
# Write binary
33+
'wb', 'bw',
34+
# Append binary
35+
'ab', 'ba',
36+
# Exclusive creation binary
37+
'xb', 'bx',
38+
# Read and write binary
39+
'rb+', 'r+b', '+rb', 'br+', 'b+r', '+br',
40+
# Write and read binary
41+
'wb+', 'w+b', '+wb', 'bw+', 'b+w', '+bw',
42+
# Append and read binary
43+
'ab+', 'a+b', '+ab', 'ba+', 'b+a', '+ba',
44+
# Exclusive creation and read binary
45+
'xb+', 'x+b', '+xb', 'bx+', 'b+x', '+bx',
10146
# Universal newline support in binary mode
47+
'rbU', 'rUb', 'Urb', 'brU', 'bUr', 'Ubr',
10248
]
10349
Filename = Union[str, pathlib.Path]
104-
IO: typing.TypeAlias = Union[typing.IO[str], typing.IO[bytes]]
50+
IO: typing.TypeAlias = Union[typing.IO[str], typing.IO[bytes]] # type: ignore[name-defined]
10551

10652

10753
class FileOpenKwargs(typing.TypedDict):

‎portalocker_tests/tests.py‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,24 @@ def test_exlusive(tmpfile):
186186
portalocker.lock(fh, portalocker.LOCK_EX | portalocker.LOCK_NB)
187187

188188
# Make sure we can't read the locked file
189-
with pytest.raises(portalocker.LockException), open(
190-
tmpfile,
191-
'r+',
192-
) as fh2:
189+
with (
190+
pytest.raises(portalocker.LockException),
191+
open(
192+
tmpfile,
193+
'r+',
194+
) as fh2,
195+
):
193196
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
194197
assert fh2.read() == text_0
195198

196199
# Make sure we can't write the locked file
197-
with pytest.raises(portalocker.LockException), open(
198-
tmpfile,
199-
'w+',
200-
) as fh2:
200+
with (
201+
pytest.raises(portalocker.LockException),
202+
open(
203+
tmpfile,
204+
'w+',
205+
) as fh2,
206+
):
201207
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
202208
fh2.write('surprise and fear')
203209

@@ -218,10 +224,13 @@ def test_shared(tmpfile):
218224
assert fh2.read() == 'spam and eggs'
219225

220226
# Make sure we can't write the locked file
221-
with pytest.raises(portalocker.LockException), open(
222-
tmpfile,
223-
'w+',
224-
) as fh2:
227+
with (
228+
pytest.raises(portalocker.LockException),
229+
open(
230+
tmpfile,
231+
'w+',
232+
) as fh2,
233+
):
225234
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
226235
fh2.write('surprise and fear')
227236

‎ruff.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
target-version = 'py39'
55

66
src = ['portalocker']
7-
exclude = ['docs']
7+
exclude = [
8+
'docs',
9+
# Ignore local test files/directories/old-stuff
10+
'test.py',
11+
'*_old.py',
12+
]
813

914
line-length = 80
1015

0 commit comments

Comments
 (0)