Skip to content

Commit

Permalink
Use explicit typing.Optional
Browse files Browse the repository at this point in the history
close #171
  • Loading branch information
sloria committed Oct 31, 2020
1 parent b9871ed commit 7001ae8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bug fixes:
Other changes:

- Test against Python 3.9.
- Remove usage of implicit `typing.Optional` ([171](https://github.com/sloria/environs/issues/171)).

## 8.0.0 (2020-05-27)

Expand Down
26 changes: 20 additions & 6 deletions environs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ class ParserConflictError(ValueError):


def _field2method(
field_or_factory: FieldOrFactory, method_name: str, *, preprocess: typing.Callable = None
field_or_factory: FieldOrFactory, method_name: str, *, preprocess: typing.Optional[typing.Callable] = None
) -> ParserMethod:
def method(
self: "Env", name: str, default: typing.Any = ma.missing, subcast: Subcast = None, **kwargs
self: "Env",
name: str,
default: typing.Any = ma.missing,
subcast: typing.Optional[Subcast] = None,
**kwargs
) -> typing.Union[_T, _Missing]:
if self._sealed:
raise EnvSealedError("Env has already been sealed. New values cannot be parsed.")
Expand Down Expand Up @@ -106,7 +110,11 @@ def method(

def _func2method(func: typing.Callable, method_name: str) -> ParserMethod:
def method(
self: "Env", name: str, default: typing.Any = ma.missing, subcast: typing.Type = None, **kwargs
self: "Env",
name: str,
default: typing.Any = ma.missing,
subcast: typing.Optional[typing.Type] = None,
**kwargs
):
if self._sealed:
raise EnvSealedError("Env has already been sealed. New values cannot be parsed.")
Expand Down Expand Up @@ -171,7 +179,7 @@ def _preprocess_dict(
value: typing.Union[str, typing.Mapping],
# TODO: Rename subcast to subcast_values and re-order arguments for next major release
subcast: Subcast,
subcast_key: Subcast = None,
subcast_key: typing.Optional[Subcast] = None,
**kwargs
) -> typing.Mapping:
if isinstance(value, Mapping):
Expand Down Expand Up @@ -239,7 +247,13 @@ def _serialize(self, value: ParseResult, *args, **kwargs) -> str:

# Override deserialize rather than _deserialize because we need
# to call urlparse *after* validation has occurred
def deserialize(self, value: str, attr: str = None, data: typing.Mapping = None, **kwargs) -> ParseResult:
def deserialize(
self,
value: str,
attr: typing.Optional[str] = None,
data: typing.Optional[typing.Mapping] = None,
**kwargs
) -> ParseResult:
ret = super().deserialize(value, attr, data, **kwargs)
return urlparse(ret)

Expand Down Expand Up @@ -301,7 +315,7 @@ def __repr__(self) -> _StrType:

@staticmethod
def read_env(
path: _StrType = None,
path: typing.Optional[_StrType] = None,
recurse: _BoolType = True,
verbose: _BoolType = False,
override: _BoolType = False,
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ ignore_missing_imports = true
warn_unreachable = true
warn_unused_ignores = true
warn_redundant_casts = true
no_implicit_optional = true

0 comments on commit 7001ae8

Please sign in to comment.