Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use --no-implicit-optional for type checking #4378

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/4378-hauntsaninja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ``--no-implicit-optional`` for type checking.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is markdown, not rst.

2 changes: 1 addition & 1 deletion pydantic/class_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def validator(
each_item: bool = False,
always: bool = False,
check_fields: bool = True,
whole: bool = None,
whole: Optional[bool] = None,
allow_reuse: bool = False,
) -> Callable[[AnyCallable], 'AnyClassMethod']:
"""
Expand Down
2 changes: 1 addition & 1 deletion pydantic/env_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def __repr__(self) -> str:


def read_env_file(
file_path: StrPath, *, encoding: str = None, case_sensitive: bool = False
file_path: StrPath, *, encoding: Optional[str] = None, case_sensitive: bool = False
) -> Dict[str, Optional[str]]:
try:
from dotenv import dotenv_values
Expand Down
38 changes: 19 additions & 19 deletions pydantic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,27 +220,27 @@ def Field(
default: Any = Undefined,
*,
default_factory: Optional[NoArgAnyCallable] = None,
alias: str = None,
title: str = None,
description: str = None,
alias: Optional[str] = None,
title: Optional[str] = None,
description: Optional[str] = None,
exclude: Union['AbstractSetIntStr', 'MappingIntStrAny', Any] = None,
include: Union['AbstractSetIntStr', 'MappingIntStrAny', Any] = None,
const: bool = None,
gt: float = None,
ge: float = None,
lt: float = None,
le: float = None,
multiple_of: float = None,
max_digits: int = None,
decimal_places: int = None,
min_items: int = None,
max_items: int = None,
unique_items: bool = None,
min_length: int = None,
max_length: int = None,
const: Optional[bool] = None,
gt: Optional[float] = None,
ge: Optional[float] = None,
lt: Optional[float] = None,
le: Optional[float] = None,
multiple_of: Optional[float] = None,
max_digits: Optional[int] = None,
decimal_places: Optional[int] = None,
min_items: Optional[int] = None,
max_items: Optional[int] = None,
unique_items: Optional[bool] = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
allow_mutation: bool = True,
regex: str = None,
discriminator: str = None,
regex: Optional[str] = None,
discriminator: Optional[str] = None,
repr: bool = True,
**extra: Any,
) -> Any:
Expand Down Expand Up @@ -395,7 +395,7 @@ def __init__(
default_factory: Optional[NoArgAnyCallable] = None,
required: 'BoolUndefined' = Undefined,
final: bool = False,
alias: str = None,
alias: Optional[str] = None,
field_info: Optional[FieldInfo] = None,
) -> None:

Expand Down
22 changes: 11 additions & 11 deletions pydantic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,9 @@ def parse_raw(
cls: Type['Model'],
b: StrBytes,
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
) -> 'Model':
try:
Expand All @@ -545,9 +545,9 @@ def parse_file(
cls: Type['Model'],
path: Union[str, Path],
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
) -> 'Model':
obj = load_file(
Expand Down Expand Up @@ -920,8 +920,8 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: None = None,
__module__: str = __name__,
__validators__: Dict[str, 'AnyClassMethod'] = None,
__cls_kwargs__: Dict[str, Any] = None,
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
__cls_kwargs__: Optional[Dict[str, Any]] = None,
**field_definitions: Any,
) -> Type['BaseModel']:
...
Expand All @@ -934,8 +934,8 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: Union[Type['Model'], Tuple[Type['Model'], ...]],
__module__: str = __name__,
__validators__: Dict[str, 'AnyClassMethod'] = None,
__cls_kwargs__: Dict[str, Any] = None,
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
__cls_kwargs__: Optional[Dict[str, Any]] = None,
**field_definitions: Any,
) -> Type['Model']:
...
Expand All @@ -947,8 +947,8 @@ def create_model(
__config__: Optional[Type[BaseConfig]] = None,
__base__: Union[None, Type['Model'], Tuple[Type['Model'], ...]] = None,
__module__: str = __name__,
__validators__: Dict[str, 'AnyClassMethod'] = None,
__cls_kwargs__: Dict[str, Any] = None,
__validators__: Optional[Dict[str, 'AnyClassMethod']] = None,
__cls_kwargs__: Optional[Dict[str, Any]] = None,
**field_definitions: Any,
) -> Type['Model']:
"""
Expand Down Expand Up @@ -1017,7 +1017,7 @@ def create_model(


def validate_model( # noqa: C901 (ignore complexity)
model: Type[BaseModel], input_data: 'DictStrAny', cls: 'ModelOrDc' = None
model: Type[BaseModel], input_data: 'DictStrAny', cls: Optional['ModelOrDc'] = None
) -> Tuple['DictStrAny', 'SetStr', Optional[ValidationError]]:
"""
validate data against a model.
Expand Down
10 changes: 5 additions & 5 deletions pydantic/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pickle
from enum import Enum
from pathlib import Path
from typing import Any, Callable, Union
from typing import Any, Callable, Optional, Union

from .types import StrBytes

Expand All @@ -15,9 +15,9 @@ class Protocol(str, Enum):
def load_str_bytes(
b: StrBytes,
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
) -> Any:
Expand Down Expand Up @@ -47,9 +47,9 @@ def load_str_bytes(
def load_file(
path: Union[str, Path],
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
) -> Any:
Expand Down
6 changes: 3 additions & 3 deletions pydantic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def field_schema(
model_name_map: Dict[TypeModelOrEnum, str],
ref_prefix: Optional[str] = None,
ref_template: str = default_ref_template,
known_models: TypeModelSet = None,
known_models: Optional[TypeModelSet] = None,
) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
"""
Process a Pydantic field and return a tuple with a JSON Schema for it as the first item.
Expand Down Expand Up @@ -344,7 +344,7 @@ def get_model_name_map(unique_models: TypeModelSet) -> Dict[TypeModelOrEnum, str
return {v: k for k, v in name_model_map.items()}


def get_flat_models_from_model(model: Type['BaseModel'], known_models: TypeModelSet = None) -> TypeModelSet:
def get_flat_models_from_model(model: Type['BaseModel'], known_models: Optional[TypeModelSet] = None) -> TypeModelSet:
"""
Take a single ``model`` and generate a set with itself and all the sub-models in the tree. I.e. if you pass
model ``Foo`` (subclass of Pydantic ``BaseModel``) as ``model``, and it has a field of type ``Bar`` (also
Expand Down Expand Up @@ -553,7 +553,7 @@ def model_process_schema(
model_name_map: Dict[TypeModelOrEnum, str],
ref_prefix: Optional[str] = None,
ref_template: str = default_ref_template,
known_models: TypeModelSet = None,
known_models: Optional[TypeModelSet] = None,
field: Optional[ModelField] = None,
) -> Tuple[Dict[str, Any], Dict[str, Any], Set[str]]:
"""
Expand Down
8 changes: 4 additions & 4 deletions pydantic/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def parse_file_as(
type_: Type[T],
path: Union[str, Path],
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
type_name: Optional[NameFactory] = None,
Expand All @@ -64,9 +64,9 @@ def parse_raw_as(
type_: Type[T],
b: StrBytes,
*,
content_type: str = None,
content_type: Optional[str] = None,
encoding: str = 'utf8',
proto: Protocol = None,
proto: Optional[Protocol] = None,
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
type_name: Optional[NameFactory] = None,
Expand Down
64 changes: 38 additions & 26 deletions pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ def __get_validators__(cls) -> 'CallableGenerator':


def conint(
*, strict: bool = False, gt: int = None, ge: int = None, lt: int = None, le: int = None, multiple_of: int = None
*,
strict: bool = False,
gt: Optional[int] = None,
ge: Optional[int] = None,
lt: Optional[int] = None,
le: Optional[int] = None,
multiple_of: Optional[int] = None,
) -> Type[int]:
# use kwargs then define conf in a dict to aid with IDE type hinting
namespace = dict(strict=strict, gt=gt, ge=ge, lt=lt, le=le, multiple_of=multiple_of)
Expand Down Expand Up @@ -296,11 +302,11 @@ def __get_validators__(cls) -> 'CallableGenerator':
def confloat(
*,
strict: bool = False,
gt: float = None,
ge: float = None,
lt: float = None,
le: float = None,
multiple_of: float = None,
gt: Optional[float] = None,
ge: Optional[float] = None,
lt: Optional[float] = None,
le: Optional[float] = None,
multiple_of: Optional[float] = None,
) -> Type[float]:
# use kwargs then define conf in a dict to aid with IDE type hinting
namespace = dict(strict=strict, gt=gt, ge=ge, lt=lt, le=le, multiple_of=multiple_of)
Expand Down Expand Up @@ -360,8 +366,8 @@ def conbytes(
strip_whitespace: bool = False,
to_upper: bool = False,
to_lower: bool = False,
min_length: int = None,
max_length: int = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
strict: bool = False,
) -> Type[bytes]:
# use kwargs then define conf in a dict to aid with IDE type hinting
Expand Down Expand Up @@ -433,10 +439,10 @@ def constr(
to_upper: bool = False,
to_lower: bool = False,
strict: bool = False,
min_length: int = None,
max_length: int = None,
curtail_length: int = None,
regex: str = None,
min_length: Optional[int] = None,
max_length: Optional[int] = None,
curtail_length: Optional[int] = None,
regex: Optional[str] = None,
) -> Type[str]:
# use kwargs then define conf in a dict to aid with IDE type hinting
namespace = dict(
Expand Down Expand Up @@ -497,7 +503,7 @@ def set_length_validator(cls, v: 'Optional[Set[T]]') -> 'Optional[Set[T]]':
return v


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


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


def conlist(
item_type: Type[T], *, min_items: int = None, max_items: int = None, unique_items: bool = None
item_type: Type[T],
*,
min_items: Optional[int] = None,
max_items: Optional[int] = None,
unique_items: Optional[bool] = None,
) -> Type[List[T]]:
# __args__ is needed to conform to typing generics api
namespace = dict(
Expand Down Expand Up @@ -704,13 +716,13 @@ def validate(cls, value: Decimal) -> Decimal:

def condecimal(
*,
gt: Decimal = None,
ge: Decimal = None,
lt: Decimal = None,
le: Decimal = None,
max_digits: int = None,
decimal_places: int = None,
multiple_of: Decimal = None,
gt: Optional[Decimal] = None,
ge: Optional[Decimal] = None,
lt: Optional[Decimal] = None,
le: Optional[Decimal] = None,
max_digits: Optional[int] = None,
decimal_places: Optional[int] = None,
multiple_of: Optional[Decimal] = None,
) -> Type[Decimal]:
# use kwargs then define conf in a dict to aid with IDE type hinting
namespace = dict(
Expand Down Expand Up @@ -1165,10 +1177,10 @@ def __get_validators__(cls) -> 'CallableGenerator':

def condate(
*,
gt: date = None,
ge: date = None,
lt: date = None,
le: date = None,
gt: Optional[date] = None,
ge: Optional[date] = None,
lt: Optional[date] = None,
le: Optional[date] = None,
) -> Type[date]:
# use kwargs then define conf in a dict to aid with IDE type hinting
namespace = dict(gt=gt, ge=ge, lt=lt, le=le)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ disallow_subclassing_any = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
disallow_untyped_calls = True
no_implicit_optional = True

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

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

[mypy-email_validator]
Expand Down