Skip to content

Commit

Permalink
Fix new issues discovered by latest mypy (0.750)
Browse files Browse the repository at this point in the history
The errors in question:

    % mypy injector
    injector/__init__.py:34: error: Incompatible types in assignment (expression has type "None", variable has type "_SpecialForm")
    injector/__init__.py:37: error: All conditional function variants must have identical signatures
    injector/__init__.py:1067: error: Argument 1 to "pop" of "MutableMapping" has incompatible type "Optional[str]"; expected "str"
    injector/__init__.py:1068: error: Argument 1 to "pop" of "MutableMapping" has incompatible type "Optional[str]"; expected "str"

Coincidentally this commit fixes an issue [1] discovered by eugenhu.
Although the solution to the issue is different than eugenhu suggests
the analysis of the underlying cause is helpful and appreciated.

[1] "Cannot inject dependency which is a subclass of a subscripted type", GH-128

Closes GH-128
  • Loading branch information
jstasiak committed Dec 9, 2019
1 parent a461f72 commit 23cd459
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions injector/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@
import types
from abc import ABCMeta, abstractmethod
from collections import namedtuple
from typing import Any, Callable, cast, Dict, Generic, List, overload, Tuple, Type, TypeVar, Union
from typing import Any, Callable, cast, Dict, Generic, List, Optional, overload, Tuple, Type, TypeVar, Union

HAVE_ANNOTATED = sys.version_info >= (3, 7, 0)

if HAVE_ANNOTATED:
# Ignoring errors here as typing_extensions stub doesn't know about those things yet
from typing_extensions import _AnnotatedAlias, Annotated, get_type_hints # type: ignore
else:
Annotated = None

class Annotated: # type: ignore
pass

from typing import get_type_hints as _get_type_hints

def get_type_hints(what, include_extras):
return _get_type_hints(what)
def get_type_hints(
obj: Callable[..., Any],
globalns: Optional[Dict[str, Any]] = None,
localns: Optional[Dict[str, Any]] = None,
include_extras: bool = False,
) -> Dict[str, Any]:
return _get_type_hints(obj, globalns, localns)


TYPING353 = hasattr(Union[str, int], '__origin__')
Expand Down Expand Up @@ -1064,8 +1072,10 @@ def _infer_injected_bindings(callable, only_explicit_bindings: bool):

# variadic arguments aren't supported at the moment (this may change
# in the future if someone has a good idea how to utilize them)
bindings.pop(spec.varargs, None)
bindings.pop(spec.varkw, None)
if spec.varargs:
bindings.pop(spec.varargs, None)
if spec.varkw:
bindings.pop(spec.varkw, None)

for k, v in list(bindings.items()):
if _is_specialization(v, Annotated):
Expand Down

0 comments on commit 23cd459

Please sign in to comment.