Skip to content

Commit 1febdc4

Browse files
committed
Use --no-implicit-optional for type checking
This makes type checking PEP 484 compliant (as of 2018). mypy will change its defaults soon. See: python/mypy#9091 python/mypy#13401
1 parent 4e78789 commit 1febdc4

File tree

9 files changed

+83
-71
lines changed

9 files changed

+83
-71
lines changed

pydantic/class_validators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def validator(
5555
each_item: bool = False,
5656
always: bool = False,
5757
check_fields: bool = True,
58-
whole: bool = None,
58+
whole: Optional[bool] = None,
5959
allow_reuse: bool = False,
6060
) -> Callable[[AnyCallable], 'AnyClassMethod']:
6161
"""

pydantic/env_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def __repr__(self) -> str:
316316

317317

318318
def read_env_file(
319-
file_path: StrPath, *, encoding: str = None, case_sensitive: bool = False
319+
file_path: StrPath, *, encoding: Optional[str] = None, case_sensitive: bool = False
320320
) -> Dict[str, Optional[str]]:
321321
try:
322322
from dotenv import dotenv_values

pydantic/fields.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,27 +220,27 @@ def Field(
220220
default: Any = Undefined,
221221
*,
222222
default_factory: Optional[NoArgAnyCallable] = None,
223-
alias: str = None,
224-
title: str = None,
225-
description: str = None,
223+
alias: Optional[str] = None,
224+
title: Optional[str] = None,
225+
description: Optional[str] = None,
226226
exclude: Union['AbstractSetIntStr', 'MappingIntStrAny', Any] = None,
227227
include: Union['AbstractSetIntStr', 'MappingIntStrAny', Any] = None,
228-
const: bool = None,
229-
gt: float = None,
230-
ge: float = None,
231-
lt: float = None,
232-
le: float = None,
233-
multiple_of: float = None,
234-
max_digits: int = None,
235-
decimal_places: int = None,
236-
min_items: int = None,
237-
max_items: int = None,
238-
unique_items: bool = None,
239-
min_length: int = None,
240-
max_length: int = None,
228+
const: Optional[bool] = None,
229+
gt: Optional[float] = None,
230+
ge: Optional[float] = None,
231+
lt: Optional[float] = None,
232+
le: Optional[float] = None,
233+
multiple_of: Optional[float] = None,
234+
max_digits: Optional[int] = None,
235+
decimal_places: Optional[int] = None,
236+
min_items: Optional[int] = None,
237+
max_items: Optional[int] = None,
238+
unique_items: Optional[bool] = None,
239+
min_length: Optional[int] = None,
240+
max_length: Optional[int] = None,
241241
allow_mutation: bool = True,
242-
regex: str = None,
243-
discriminator: str = None,
242+
regex: Optional[str] = None,
243+
discriminator: Optional[str] = None,
244244
repr: bool = True,
245245
**extra: Any,
246246
) -> Any:
@@ -395,7 +395,7 @@ def __init__(
395395
default_factory: Optional[NoArgAnyCallable] = None,
396396
required: 'BoolUndefined' = Undefined,
397397
final: bool = False,
398-
alias: str = None,
398+
alias: Optional[str] = None,
399399
field_info: Optional[FieldInfo] = None,
400400
) -> None:
401401

pydantic/main.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,9 @@ def parse_raw(
522522
cls: Type['Model'],
523523
b: StrBytes,
524524
*,
525-
content_type: str = None,
525+
content_type: Optional[str] = None,
526526
encoding: str = 'utf8',
527-
proto: Protocol = None,
527+
proto: Optional[Protocol] = None,
528528
allow_pickle: bool = False,
529529
) -> 'Model':
530530
try:
@@ -545,9 +545,9 @@ def parse_file(
545545
cls: Type['Model'],
546546
path: Union[str, Path],
547547
*,
548-
content_type: str = None,
548+
content_type: Optional[str] = None,
549549
encoding: str = 'utf8',
550-
proto: Protocol = None,
550+
proto: Optional[Protocol] = None,
551551
allow_pickle: bool = False,
552552
) -> 'Model':
553553
obj = load_file(
@@ -920,8 +920,8 @@ def create_model(
920920
__config__: Optional[Type[BaseConfig]] = None,
921921
__base__: None = None,
922922
__module__: str = __name__,
923-
__validators__: Dict[str, 'AnyClassMethod'] = None,
924-
__cls_kwargs__: Dict[str, Any] = None,
923+
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
924+
__cls_kwargs__: Optional[Dict[str, Any]] = None,
925925
**field_definitions: Any,
926926
) -> Type['BaseModel']:
927927
...
@@ -934,8 +934,8 @@ def create_model(
934934
__config__: Optional[Type[BaseConfig]] = None,
935935
__base__: Union[Type['Model'], Tuple[Type['Model'], ...]],
936936
__module__: str = __name__,
937-
__validators__: Dict[str, 'AnyClassMethod'] = None,
938-
__cls_kwargs__: Dict[str, Any] = None,
937+
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
938+
__cls_kwargs__: Optional[Dict[str, Any]] = None,
939939
**field_definitions: Any,
940940
) -> Type['Model']:
941941
...
@@ -947,8 +947,8 @@ def create_model(
947947
__config__: Optional[Type[BaseConfig]] = None,
948948
__base__: Union[None, Type['Model'], Tuple[Type['Model'], ...]] = None,
949949
__module__: str = __name__,
950-
__validators__: Dict[str, 'AnyClassMethod'] = None,
951-
__cls_kwargs__: Dict[str, Any] = None,
950+
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
951+
__cls_kwargs__: Optional[Dict[str, Any]] = None,
952952
**field_definitions: Any,
953953
) -> Type['Model']:
954954
"""
@@ -1017,7 +1017,7 @@ def create_model(
10171017

10181018

10191019
def validate_model( # noqa: C901 (ignore complexity)
1020-
model: Type[BaseModel], input_data: 'DictStrAny', cls: 'ModelOrDc' = None
1020+
model: Type[BaseModel], input_data: 'DictStrAny', cls: Optional['ModelOrDc'] = None
10211021
) -> Tuple['DictStrAny', 'SetStr', Optional[ValidationError]]:
10221022
"""
10231023
validate data against a model.

pydantic/parse.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pickle
33
from enum import Enum
44
from pathlib import Path
5-
from typing import Any, Callable, Union
5+
from typing import Any, Callable, Optional, Union
66

77
from .types import StrBytes
88

@@ -15,9 +15,9 @@ class Protocol(str, Enum):
1515
def load_str_bytes(
1616
b: StrBytes,
1717
*,
18-
content_type: str = None,
18+
content_type: Optional[str] = None,
1919
encoding: str = 'utf8',
20-
proto: Protocol = None,
20+
proto: Optional[Protocol] = None,
2121
allow_pickle: bool = False,
2222
json_loads: Callable[[str], Any] = json.loads,
2323
) -> Any:
@@ -47,9 +47,9 @@ def load_str_bytes(
4747
def load_file(
4848
path: Union[str, Path],
4949
*,
50-
content_type: str = None,
50+
content_type: Optional[str] = None,
5151
encoding: str = 'utf8',
52-
proto: Protocol = None,
52+
proto: Optional[Protocol] = None,
5353
allow_pickle: bool = False,
5454
json_loads: Callable[[str], Any] = json.loads,
5555
) -> Any:

pydantic/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def field_schema(
224224
model_name_map: Dict[TypeModelOrEnum, str],
225225
ref_prefix: Optional[str] = None,
226226
ref_template: str = default_ref_template,
227-
known_models: TypeModelSet = None,
227+
known_models: Optional[TypeModelSet] = None,
228228
) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
229229
"""
230230
Process a Pydantic field and return a tuple with a JSON Schema for it as the first item.
@@ -344,7 +344,7 @@ def get_model_name_map(unique_models: TypeModelSet) -> Dict[TypeModelOrEnum, str
344344
return {v: k for k, v in name_model_map.items()}
345345

346346

347-
def get_flat_models_from_model(model: Type['BaseModel'], known_models: TypeModelSet = None) -> TypeModelSet:
347+
def get_flat_models_from_model(model: Type['BaseModel'], known_models: Optional[TypeModelSet] = None) -> TypeModelSet:
348348
"""
349349
Take a single ``model`` and generate a set with itself and all the sub-models in the tree. I.e. if you pass
350350
model ``Foo`` (subclass of Pydantic ``BaseModel``) as ``model``, and it has a field of type ``Bar`` (also
@@ -553,7 +553,7 @@ def model_process_schema(
553553
model_name_map: Dict[TypeModelOrEnum, str],
554554
ref_prefix: Optional[str] = None,
555555
ref_template: str = default_ref_template,
556-
known_models: TypeModelSet = None,
556+
known_models: Optional[TypeModelSet] = None,
557557
field: Optional[ModelField] = None,
558558
) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
559559
"""

pydantic/tools.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def parse_file_as(
4242
type_: Type[T],
4343
path: Union[str, Path],
4444
*,
45-
content_type: str = None,
45+
content_type: Optional[str] = None,
4646
encoding: str = 'utf8',
47-
proto: Protocol = None,
47+
proto: Optional[Protocol] = None,
4848
allow_pickle: bool = False,
4949
json_loads: Callable[[str], Any] = json.loads,
5050
type_name: Optional[NameFactory] = None,
@@ -64,9 +64,9 @@ def parse_raw_as(
6464
type_: Type[T],
6565
b: StrBytes,
6666
*,
67-
content_type: str = None,
67+
content_type: Optional[str] = None,
6868
encoding: str = 'utf8',
69-
proto: Protocol = None,
69+
proto: Optional[Protocol] = None,
7070
allow_pickle: bool = False,
7171
json_loads: Callable[[str], Any] = json.loads,
7272
type_name: Optional[NameFactory] = None,

pydantic/types.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,13 @@ def __get_validators__(cls) -> 'CallableGenerator':
224224

225225

226226
def conint(
227-
*, strict: bool = False, gt: int = None, ge: int = None, lt: int = None, le: int = None, multiple_of: int = None
227+
*,
228+
strict: bool = False,
229+
gt: Optional[int] = None,
230+
ge: Optional[int] = None,
231+
lt: Optional[int] = None,
232+
le: Optional[int] = None,
233+
multiple_of: Optional[int] = None,
228234
) -> Type[int]:
229235
# use kwargs then define conf in a dict to aid with IDE type hinting
230236
namespace = dict(strict=strict, gt=gt, ge=ge, lt=lt, le=le, multiple_of=multiple_of)
@@ -296,11 +302,11 @@ def __get_validators__(cls) -> 'CallableGenerator':
296302
def confloat(
297303
*,
298304
strict: bool = False,
299-
gt: float = None,
300-
ge: float = None,
301-
lt: float = None,
302-
le: float = None,
303-
multiple_of: float = None,
305+
gt: Optional[float] = None,
306+
ge: Optional[float] = None,
307+
lt: Optional[float] = None,
308+
le: Optional[float] = None,
309+
multiple_of: Optional[float] = None,
304310
) -> Type[float]:
305311
# use kwargs then define conf in a dict to aid with IDE type hinting
306312
namespace = dict(strict=strict, gt=gt, ge=ge, lt=lt, le=le, multiple_of=multiple_of)
@@ -360,8 +366,8 @@ def conbytes(
360366
strip_whitespace: bool = False,
361367
to_upper: bool = False,
362368
to_lower: bool = False,
363-
min_length: int = None,
364-
max_length: int = None,
369+
min_length: Optional[int] = None,
370+
max_length: Optional[int] = None,
365371
strict: bool = False,
366372
) -> Type[bytes]:
367373
# use kwargs then define conf in a dict to aid with IDE type hinting
@@ -433,10 +439,10 @@ def constr(
433439
to_upper: bool = False,
434440
to_lower: bool = False,
435441
strict: bool = False,
436-
min_length: int = None,
437-
max_length: int = None,
438-
curtail_length: int = None,
439-
regex: str = None,
442+
min_length: Optional[int] = None,
443+
max_length: Optional[int] = None,
444+
curtail_length: Optional[int] = None,
445+
regex: Optional[str] = None,
440446
) -> Type[str]:
441447
# use kwargs then define conf in a dict to aid with IDE type hinting
442448
namespace = dict(
@@ -497,7 +503,7 @@ def set_length_validator(cls, v: 'Optional[Set[T]]') -> 'Optional[Set[T]]':
497503
return v
498504

499505

500-
def conset(item_type: Type[T], *, min_items: int = None, max_items: int = None) -> Type[Set[T]]:
506+
def conset(item_type: Type[T], *, min_items: Optional[int] = None, max_items: Optional[int] = None) -> Type[Set[T]]:
501507
# __args__ is needed to conform to typing generics api
502508
namespace = {'min_items': min_items, 'max_items': max_items, 'item_type': item_type, '__args__': [item_type]}
503509
# We use new_class to be able to deal with Generic types
@@ -539,7 +545,9 @@ def frozenset_length_validator(cls, v: 'Optional[FrozenSet[T]]') -> 'Optional[Fr
539545
return v
540546

541547

542-
def confrozenset(item_type: Type[T], *, min_items: int = None, max_items: int = None) -> Type[FrozenSet[T]]:
548+
def confrozenset(
549+
item_type: Type[T], *, min_items: Optional[int] = None, max_items: Optional[int] = None
550+
) -> Type[FrozenSet[T]]:
543551
# __args__ is needed to conform to typing generics api
544552
namespace = {'min_items': min_items, 'max_items': max_items, 'item_type': item_type, '__args__': [item_type]}
545553
# We use new_class to be able to deal with Generic types
@@ -595,7 +603,11 @@ def unique_items_validator(cls, v: 'List[T]') -> 'List[T]':
595603

596604

597605
def conlist(
598-
item_type: Type[T], *, min_items: int = None, max_items: int = None, unique_items: bool = None
606+
item_type: Type[T],
607+
*,
608+
min_items: Optional[int] = None,
609+
max_items: Optional[int] = None,
610+
unique_items: Optional[bool] = None,
599611
) -> Type[List[T]]:
600612
# __args__ is needed to conform to typing generics api
601613
namespace = dict(
@@ -704,13 +716,13 @@ def validate(cls, value: Decimal) -> Decimal:
704716

705717
def condecimal(
706718
*,
707-
gt: Decimal = None,
708-
ge: Decimal = None,
709-
lt: Decimal = None,
710-
le: Decimal = None,
711-
max_digits: int = None,
712-
decimal_places: int = None,
713-
multiple_of: Decimal = None,
719+
gt: Optional[Decimal] = None,
720+
ge: Optional[Decimal] = None,
721+
lt: Optional[Decimal] = None,
722+
le: Optional[Decimal] = None,
723+
max_digits: Optional[int] = None,
724+
decimal_places: Optional[int] = None,
725+
multiple_of: Optional[Decimal] = None,
714726
) -> Type[Decimal]:
715727
# use kwargs then define conf in a dict to aid with IDE type hinting
716728
namespace = dict(
@@ -1165,10 +1177,10 @@ def __get_validators__(cls) -> 'CallableGenerator':
11651177

11661178
def condate(
11671179
*,
1168-
gt: date = None,
1169-
ge: date = None,
1170-
lt: date = None,
1171-
le: date = None,
1180+
gt: Optional[date] = None,
1181+
ge: Optional[date] = None,
1182+
lt: Optional[date] = None,
1183+
le: Optional[date] = None,
11721184
) -> Type[date]:
11731185
# use kwargs then define conf in a dict to aid with IDE type hinting
11741186
namespace = dict(gt=gt, ge=ge, lt=lt, le=le)

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ disallow_subclassing_any = True
6666
disallow_incomplete_defs = True
6767
disallow_untyped_decorators = True
6868
disallow_untyped_calls = True
69+
no_implicit_optional = True
6970

7071
# for strict mypy: (this is the tricky one :-))
7172
disallow_untyped_defs = True
7273

7374
# remaining arguments from `mypy --strict` which cause errors
74-
;no_implicit_optional = True
7575
;warn_return_any = True
7676

7777
[mypy-email_validator]

0 commit comments

Comments
 (0)