Skip to content

Fix if statements with ambiguously indented bodies #360

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

Merged
merged 2 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,17 @@ def _type_check(arg, msg):
return type(None)
if isinstance(arg, basestring):
arg = _ForwardRef(arg)
if (isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
not isinstance(arg, (type, _TypingBase)) and not callable(arg)):
if (
isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
not isinstance(arg, (type, _TypingBase)) and not callable(arg)
):
raise TypeError(msg + " Got %.100r." % (arg,))
# Bare Union etc. are not valid as type arguments
if (type(arg).__name__ in ('_Union', '_Optional')
and not getattr(arg, '__origin__', None)
or isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)):
if (
type(arg).__name__ in ('_Union', '_Optional') and
not getattr(arg, '__origin__', None) or
isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)
):
raise TypeError("Plain %s is not valid as type argument" % arg)
return arg

Expand Down Expand Up @@ -929,8 +933,10 @@ def _valid_for_check(cls):
if cls is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % cls)
if (cls.__origin__ is not None and
sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']):
if (
cls.__origin__ is not None and
sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']
):
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")

Expand Down Expand Up @@ -1057,9 +1063,12 @@ def __new__(cls, name, bases, namespace,
# This allows unparameterized generic collections to be used
# with issubclass() and isinstance() in the same way as their
# collections.abc counterparts (e.g., isinstance([], Iterable)).
if ('__subclasshook__' not in namespace and extra # allow overriding
or hasattr(self.__subclasshook__, '__name__') and
self.__subclasshook__.__name__ == '__extrahook__'):
if (
# allow overriding
'__subclasshook__' not in namespace and extra or
hasattr(self.__subclasshook__, '__name__') and
self.__subclasshook__.__name__ == '__extrahook__'
):
self.__subclasshook__ = _make_subclasshook(self)

if origin and hasattr(origin, '__qualname__'): # Fix for Python 3.2.
Expand Down
35 changes: 23 additions & 12 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,17 @@ def _type_check(arg, msg):
return type(None)
if isinstance(arg, str):
arg = _ForwardRef(arg)
if (isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
not isinstance(arg, (type, _TypingBase)) and not callable(arg)):
if (
isinstance(arg, _TypingBase) and type(arg).__name__ == '_ClassVar' or
not isinstance(arg, (type, _TypingBase)) and not callable(arg)
):
raise TypeError(msg + " Got %.100r." % (arg,))
# Bare Union etc. are not valid as type arguments
if (type(arg).__name__ in ('_Union', '_Optional')
and not getattr(arg, '__origin__', None)
or isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)):
if (
type(arg).__name__ in ('_Union', '_Optional') and
not getattr(arg, '__origin__', None) or
isinstance(arg, TypingMeta) and _gorg(arg) in (Generic, _Protocol)
):
raise TypeError("Plain %s is not valid as type argument" % arg)
return arg

Expand Down Expand Up @@ -850,8 +854,10 @@ def _valid_for_check(cls):
if cls is Generic:
raise TypeError("Class %r cannot be used with class "
"or instance checks" % cls)
if (cls.__origin__ is not None and
sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']):
if (
cls.__origin__ is not None and
sys._getframe(3).f_globals['__name__'] not in ['abc', 'functools']
):
raise TypeError("Parameterized generics cannot be used with class "
"or instance checks")

Expand Down Expand Up @@ -987,9 +993,12 @@ def __new__(cls, name, bases, namespace,
# This allows unparameterized generic collections to be used
# with issubclass() and isinstance() in the same way as their
# collections.abc counterparts (e.g., isinstance([], Iterable)).
if ('__subclasshook__' not in namespace and extra # allow overriding
or hasattr(self.__subclasshook__, '__name__') and
self.__subclasshook__.__name__ == '__extrahook__'):
if (
# allow overriding
'__subclasshook__' not in namespace and extra or
hasattr(self.__subclasshook__, '__name__') and
self.__subclasshook__.__name__ == '__extrahook__'
):
self.__subclasshook__ = _make_subclasshook(self)
if isinstance(extra, abc.ABCMeta):
self._abc_registry = extra._abc_registry
Expand Down Expand Up @@ -1443,10 +1452,12 @@ def get_type_hints(obj, globalns=None, localns=None):
hints = getattr(obj, '__annotations__', None)
if hints is None:
# Return empty annotations for something that _could_ have them.
if (isinstance(obj, types.FunctionType) or
if (
isinstance(obj, types.FunctionType) or
isinstance(obj, types.BuiltinFunctionType) or
isinstance(obj, types.MethodType) or
isinstance(obj, types.ModuleType)):
isinstance(obj, types.ModuleType)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe this could be rewritten as isinstance(obj, (types.FunctionType, types.BuiltinFunctionType, types.MethodType, types.ModuleType))?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, I tried to make minimal changes to make the review easier.

):
return {}
else:
raise TypeError('{!r} is not a module, class, method, '
Expand Down
2 changes: 0 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ changedir = python2
builtins = basestring, unicode
ignore =
# temporary ignores until we sort it out
E129,
E501,
W503,
# irrelevant plugins
B3,
DW12
Expand Down