Skip to content

Commit edb6029

Browse files
Евгений БлиновЕвгений Блинов
authored andcommitted
Lint's issues
1 parent e1ee11d commit edb6029

8 files changed

Lines changed: 81 additions & 91 deletions

File tree

simtypes/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from simtypes.check import check as check # noqa: F401
2-
from simtypes.from_string import from_string as from_string # noqa: F401
3-
from simtypes.types.ints.natural import NaturalNumber as NaturalNumber # noqa: F401
4-
from simtypes.types.ints.non_negative import NonNegativeInt as NonNegativeInt # noqa: F401
1+
from simtypes.check import check as check
2+
from simtypes.from_string import from_string as from_string
3+
from simtypes.types.ints.natural import NaturalNumber as NaturalNumber
4+
from simtypes.types.ints.non_negative import (
5+
NonNegativeInt as NonNegativeInt,
6+
)

simtypes/check.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from inspect import isclass
2-
from unittest.mock import Mock, MagicMock
2+
from unittest.mock import MagicMock, Mock
33

44
try:
55
from types import UnionType # type: ignore[attr-defined, unused-ignore]
@@ -11,48 +11,45 @@
1111
except ImportError: # pragma: no cover
1212
from typing_extensions import TypeIs
1313

14-
from typing import List, Type, Union, Any, get_args, get_origin
14+
from typing import Any, List, Type, Union, get_args, get_origin
1515

1616
from denial import InnerNoneType
1717

1818
from simtypes.typing import ExpectedType
1919

2020

21-
def check(value: Any, type_hint: Type[ExpectedType], strict: bool = False, lists_are_tuples: bool = False, pass_mocks: bool = True) -> TypeIs[ExpectedType]:
22-
if type_hint is Any: # type: ignore[comparison-overlap]
21+
def check(value: Any, type_hint: Type[ExpectedType], strict: bool = False, lists_are_tuples: bool = False, pass_mocks: bool = True) -> TypeIs[ExpectedType]: # noqa: C901, PLR0911, PLR0912
22+
if type_hint is Any or (isinstance(value, (Mock, MagicMock)) and pass_mocks): # type: ignore[comparison-overlap]
2323
return True
2424

25-
elif (isinstance(value, Mock) or isinstance(value, MagicMock)) and pass_mocks:
26-
return True
27-
28-
elif type_hint is None:
25+
if type_hint is None:
2926
return value is None
3027

31-
elif isinstance(type_hint, InnerNoneType):
28+
if isinstance(type_hint, InnerNoneType):
3229
return type_hint == value
3330

3431
origin_type = get_origin(type_hint)
3532

3633
if origin_type is Union or origin_type is UnionType:
3734
return any(check(value, argument, strict=strict, lists_are_tuples=lists_are_tuples) for argument in get_args(type_hint))
3835

39-
elif origin_type is list and strict:
36+
if origin_type is list and strict:
4037
if not isinstance(value, list):
4138
return False
4239
arguments = get_args(type_hint)
4340
if not arguments:
4441
return True
4542
return all(check(subvalue, arguments[0], strict=strict, lists_are_tuples=lists_are_tuples) for subvalue in value)
4643

47-
elif origin_type is dict and strict:
44+
if origin_type is dict and strict:
4845
if not isinstance(value, dict):
4946
return False
5047
arguments = get_args(type_hint)
5148
if not arguments:
5249
return True
5350
return all(check(key, arguments[0], strict=strict, lists_are_tuples=lists_are_tuples) and check(subvalue, arguments[1], strict=strict, lists_are_tuples=lists_are_tuples) for key, subvalue in value.items())
5451

55-
elif origin_type is tuple and strict:
52+
if origin_type is tuple and strict:
5653
types_to_check: List[Union[Type[list], Type[tuple]]] = [tuple] if not lists_are_tuples else [tuple, list] # type: ignore[type-arg]
5754
if all(not isinstance(value, x) for x in types_to_check):
5855
return False
@@ -70,14 +67,13 @@ def check(value: Any, type_hint: Type[ExpectedType], strict: bool = False, lists
7067

7168
return all(check(subvalue, expected_subtype, strict=strict, lists_are_tuples=lists_are_tuples) for subvalue, expected_subtype in zip(value, arguments))
7269

73-
else:
74-
if origin_type is not None:
75-
return isinstance(value, origin_type)
70+
if origin_type is not None:
71+
return isinstance(value, origin_type)
7672

77-
if not isclass(type_hint):
78-
raise ValueError('Type must be a valid type object.')
73+
if not isclass(type_hint):
74+
raise ValueError('Type must be a valid type object.')
7975

80-
if type_hint is tuple and lists_are_tuples:
81-
return isinstance(value, tuple) or isinstance(value, list) # pragma: no cover
76+
if type_hint is tuple and lists_are_tuples:
77+
return isinstance(value, (tuple, list)) # pragma: no cover
8278

83-
return isinstance(value, type_hint)
79+
return isinstance(value, type_hint)

simtypes/from_string.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
from typing import List, Tuple, Dict, Type, Optional, Union, Any, get_origin, get_args
2-
from json import loads, JSONDecodeError
3-
from inspect import isclass
4-
from datetime import datetime, date
51
from collections.abc import Hashable
2+
from datetime import date, datetime
3+
from inspect import isclass
4+
from json import JSONDecodeError, loads
5+
from typing import Any, Dict, List, Optional, Tuple, Type, Union, get_args, get_origin
66

77
from simtypes import check
88
from simtypes.typing import ExpectedType
99

1010

11-
def convert_single_value(value: str, expected_type: Type[ExpectedType]) -> ExpectedType:
11+
def convert_single_value(value: str, expected_type: Type[ExpectedType]) -> ExpectedType: # noqa: PLR0912, PLR0911, C901
1212
if expected_type is str:
1313
return value # type: ignore[return-value]
1414

15-
elif expected_type is bool:
15+
if expected_type is bool:
1616
if value in ('True', 'true', 'yes'):
1717
return True # type: ignore[return-value]
18-
elif value in ('False', 'false', 'no'):
18+
if value in ('False', 'false', 'no'):
1919
return False # type: ignore[return-value]
20-
else:
21-
raise TypeError(f'The string "{value}" cannot be interpreted as a boolean value.')
20+
raise TypeError(f'The string "{value}" cannot be interpreted as a boolean value.')
2221

23-
elif expected_type is int:
22+
if expected_type is int:
2423
try:
2524
return int(value) # type: ignore[return-value]
2625
except ValueError as e:
2726
raise TypeError(f'The string "{value}" cannot be interpreted as an integer.') from e
2827

2928
elif expected_type is float:
30-
if value == '∞' or value == '+∞':
29+
if value in {'∞', '+∞'}:
3130
value = 'inf'
3231
elif value == '-∞':
3332
value = '-inf'
@@ -87,7 +86,7 @@ def fix_lists(collection: List[Any], type_hint_arguments: Tuple[Any, ...]) -> Op
8786
return result
8887

8988

90-
def fix_tuples(collection: List[Any], type_hint_arguments: Tuple[Any, ...]) -> Optional[Tuple[Any, ...]]:
89+
def fix_tuples(collection: List[Any], type_hint_arguments: Tuple[Any, ...]) -> Optional[Tuple[Any, ...]]: # noqa: PLR0912, PLR0911, C901
9190
if not isinstance(collection, list):
9291
return None
9392

@@ -158,7 +157,7 @@ def fix_dicts(collection: List[Any], type_hint_arguments: Tuple[Any, ...]) -> Op
158157
pair_result = {}
159158

160159
for name, meta in pair.items():
161-
element, type_hint = meta
160+
element, type_hint = meta # noqa: PLW2901
162161
origin_type = get_origin(type_hint)
163162
type_hint_arguments = get_args(type_hint)
164163
if any(x in (dict, list, tuple) for x in (type_hint, origin_type)):
@@ -221,7 +220,6 @@ def from_string(value: str, expected_type: Type[ExpectedType]) -> ExpectedType:
221220

222221
if check(result, expected_type, strict=True): # type: ignore[operator]
223222
return result # type: ignore[no-any-return]
224-
else:
225-
raise error
223+
raise error
226224

227225
return convert_single_value(value, expected_type)

simtypes/types/strings/email.py

Whitespace-only changes.

simtypes/typing.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from typing import TypeVar
22

3-
43
ExpectedType = TypeVar('ExpectedType')

tests/units/conftest.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import sys
2-
from typing import Tuple, List, Set, Dict, Union, Optional
2+
from typing import Dict, List, Optional, Set, Tuple, Union
33

44
import pytest
55

66

7-
@pytest.fixture(params=[1, 2])
8-
def new_style(request):
9-
return request.param
10-
11-
127
@pytest.fixture(params=[Dict, dict])
138
def dict_type(request):
149
return request.param

tests/units/test_check.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import sys
2-
from unittest.mock import Mock, MagicMock
2+
from unittest.mock import MagicMock, Mock
33

44
try:
55
from types import NoneType # type: ignore[attr-defined]
66
except ImportError:
77
NoneType = type(None) # type: ignore[misc]
88

9-
from typing import Optional, Any, Union
109
from collections.abc import Sequence
10+
from typing import Any, Optional, Union
1111

1212
import pytest
13-
from full_match import match
1413
from denial import InnerNone, InnerNoneType, SentinelType
14+
from full_match import match
1515

1616
from simtypes import check
1717

@@ -132,7 +132,7 @@ def test_bool_is_int(make_optional, make_union):
132132
assert check(False, make_optional(make_union(int, str)))
133133

134134

135-
def test_optional(new_style, tuple_type, list_type, make_optional):
135+
def test_optional(tuple_type, list_type, make_optional):
136136
assert check(None, make_optional(int))
137137
assert check(1, make_optional(int))
138138
assert check(0, make_optional(int))
@@ -185,11 +185,11 @@ def test_optional_union(make_union, make_optional, tuple_type):
185185

186186

187187
@pytest.mark.parametrize(
188-
['addictional_parameters'],
188+
'addictional_parameters',
189189
[
190-
({},),
191-
({'strict': True},),
192-
({'strict': False},),
190+
{},
191+
{'strict': True},
192+
{'strict': False},
193193
],
194194
)
195195
def test_list_without_arguments(list_type, addictional_parameters):
@@ -210,11 +210,11 @@ def test_list_without_arguments(list_type, addictional_parameters):
210210

211211

212212
@pytest.mark.parametrize(
213-
['addictional_parameters'],
213+
'addictional_parameters',
214214
[
215-
({},),
216-
({'strict': True},),
217-
({'strict': False},),
215+
{},
216+
{'strict': True},
217+
{'strict': False},
218218
],
219219
)
220220
def test_tuple_without_arguments(tuple_type, addictional_parameters):
@@ -240,11 +240,11 @@ def test_tuple_without_arguments(tuple_type, addictional_parameters):
240240

241241

242242
@pytest.mark.parametrize(
243-
['addictional_parameters'],
243+
'addictional_parameters',
244244
[
245-
({},),
246-
({'strict': True},),
247-
({'strict': False},),
245+
{},
246+
{'strict': True},
247+
{'strict': False},
248248
],
249249
)
250250
def test_set_without_arguments(set_type, addictional_parameters):
@@ -265,11 +265,11 @@ def test_set_without_arguments(set_type, addictional_parameters):
265265

266266

267267
@pytest.mark.parametrize(
268-
['addictional_parameters'],
268+
'addictional_parameters',
269269
[
270-
({},),
271-
({'strict': True},),
272-
({'strict': False},),
270+
{},
271+
{'strict': True},
272+
{'strict': False},
273273
],
274274
)
275275
def test_dict_without_arguments(dict_type, addictional_parameters):
@@ -449,17 +449,17 @@ def test_lists_are_tuples_flag_is_true_in_strict_mode(subscribable_tuple_type, s
449449

450450

451451
@pytest.mark.parametrize(
452-
['strict_mode'],
452+
'strict_mode',
453453
[
454-
(False,),
455-
(True,),
454+
False,
455+
True,
456456
],
457457
)
458458
@pytest.mark.parametrize(
459-
['addictional_parameters'],
459+
'addictional_parameters',
460460
[
461-
({'pass_mocks': True},),
462-
({},),
461+
{'pass_mocks': True},
462+
{},
463463
],
464464
)
465465
def test_pass_mocks_when_its_on(strict_mode, list_type, addictional_parameters):
@@ -476,10 +476,10 @@ def test_pass_mocks_when_its_on(strict_mode, list_type, addictional_parameters):
476476

477477

478478
@pytest.mark.parametrize(
479-
['strict_mode'],
479+
'strict_mode',
480480
[
481-
(False,),
482-
(True,),
481+
False,
482+
True,
483483
],
484484
)
485485
def test_pass_mocks_when_its_off(strict_mode, list_type):
@@ -496,10 +496,10 @@ def test_pass_mocks_when_its_off(strict_mode, list_type):
496496

497497

498498
@pytest.mark.parametrize(
499-
['strict_mode'],
499+
'strict_mode',
500500
[
501-
(False,),
502-
(True,),
501+
False,
502+
True,
503503
],
504504
)
505505
def test_denial_sentinel(strict_mode):
@@ -514,10 +514,10 @@ def test_denial_sentinel(strict_mode):
514514

515515

516516
@pytest.mark.parametrize(
517-
['strict_mode'],
517+
'strict_mode',
518518
[
519-
(False,),
520-
(True,),
519+
False,
520+
True,
521521
],
522522
)
523523
def test_denial_innernonetype(strict_mode):
@@ -532,10 +532,10 @@ def test_denial_innernonetype(strict_mode):
532532

533533

534534
@pytest.mark.parametrize(
535-
['strict_mode'],
535+
'strict_mode',
536536
[
537-
(False,),
538-
(True,),
537+
False,
538+
True,
539539
],
540540
)
541541
def test_denial_innernone(strict_mode):

tests/units/test_from_string.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from math import inf, isnan
2-
from typing import Any
31
from datetime import date, datetime
42
from json import dumps
3+
from math import inf, isnan
4+
from typing import Any
55

66
import pytest
77
from full_match import match
@@ -299,11 +299,11 @@ def test_get_dict_value(dict_type, subscribable_list_type, subscribable_dict_typ
299299

300300

301301
@pytest.mark.parametrize(
302-
['string'],
302+
'string',
303303
[
304-
('{"lol": "kek"}',),
305-
('1',),
306-
('kek',),
304+
'{"lol": "kek"}',
305+
'1',
306+
'kek',
307307
],
308308
)
309309
def test_get_any(string):
@@ -350,7 +350,7 @@ def test_deserialize_subscribable_collections_with_dates(subscribable_list_type,
350350
assert from_string(dumps({isoformatted_date: isoformatted_date}), subscribable_dict_type[str, date]) == {isoformatted_date: date.fromisoformat(isoformatted_date)}
351351

352352

353-
def test_wrong_collection_content(subscribable_list_type, subscribable_tuple_type, subscribable_dict_type, dict_type, list_type, tuple_type):
353+
def test_wrong_collection_content(subscribable_list_type, subscribable_tuple_type, subscribable_dict_type, dict_type, list_type, tuple_type): # noqa: PLR0915, PLR0913
354354
with pytest.raises(TypeError, match=match('The string "[123]" cannot be interpreted as a list of the specified format.')):
355355
from_string(dumps([123]), subscribable_list_type[date])
356356

0 commit comments

Comments
 (0)