diff --git a/tests/functional/g/generic_alias/__init__.py b/tests/functional/g/generic_alias/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/functional/g/generic_alias/generic_alias_collections.py b/tests/functional/g/generic_alias/generic_alias_collections.py new file mode 100644 index 0000000000..33f6b6e2c3 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections.py @@ -0,0 +1,130 @@ +"""Test generic alias support for stdlib types (added in PY39).""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +import abc +import collections +import collections.abc +import contextlib +import re + +# special +tuple[int, int] +type[int] +collections.abc.Callable[[int], str] + +# builtins +dict[int, str] +list[int] +set[int] +frozenset[int] + +# collections +collections.defaultdict[int, str] +collections.OrderedDict[int, str] +collections.ChainMap[int, str] +collections.Counter[int] +collections.deque[int] + +# collections.abc +collections.abc.Set[int] +collections.abc.Collection[int] +collections.abc.Container[int] +collections.abc.ItemsView[int, str] +collections.abc.KeysView[int] +collections.abc.Mapping[int, str] +collections.abc.MappingView[int] +collections.abc.MutableMapping[int, str] +collections.abc.MutableSequence[int] +collections.abc.MutableSet[int] +collections.abc.Sequence[int] +collections.abc.ValuesView[int] + +collections.abc.Iterable[int] +collections.abc.Iterator[int] +collections.abc.Generator[int, None, None] +collections.abc.Reversible[int] + +collections.abc.Coroutine[list[str], str, int] +collections.abc.AsyncGenerator[int, None] +collections.abc.AsyncIterable[int] +collections.abc.AsyncIterator[int] +collections.abc.Awaitable[int] + +# contextlib +contextlib.AbstractContextManager[int] +contextlib.AbstractAsyncContextManager[int] + +# re +re.Pattern[str] +re.Match[str] + + +# unsubscriptable types +collections.abc.Hashable +collections.abc.Sized +collections.abc.Hashable[int] # [unsubscriptable-object] +collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +collections.abc.ByteString[int] + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(collections.abc.Iterable[int]): # [abstract-method] # __iter__ + pass + +class DerivedCollection(collections.abc.Collection[int]): # [abstract-method,abstract-method,abstract-method] # __contains__, __iter__, __len__ + pass + + +# No implementation required for 'builtins' and 'collections' types +class DerivedList(list[int]): + pass + +class DerivedSet(set[int]): + pass + +class DerivedOrderedDict(collections.OrderedDict[int, str]): + pass + +class DerivedListIterable(list[collections.abc.Iterable[int]]): + pass + + +# Multiple generic base classes +class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method] + pass + +class CustomAbstractCls1(abc.ABC): + pass +class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,abstract-method] # __iter__, __len__ + pass +class CustomImplementation(CustomAbstractCls2): # [abstract-method,abstract-method] # __iter__, __len__ + pass + + +# Type annotations +var_tuple: tuple[int, int] +var_dict: dict[int, str] +var_orderedDict: collections.OrderedDict[int, str] +var_container: collections.abc.Container[int] +var_sequence: collections.abc.Sequence[int] +var_iterable: collections.abc.Iterable[int] +var_awaitable: collections.abc.Awaitable[int] +var_contextmanager: contextlib.AbstractContextManager[int] +var_pattern: re.Pattern[int] +var_bytestring: collections.abc.ByteString +var_hashable: collections.abc.Hashable +var_sized: collections.abc.Sized + +# Type annotation with unsubscriptable type +var_int: int[int] # [unsubscriptable-object] +var_hashable2: collections.abc.Hashable[int] # [unsubscriptable-object] +var_sized2: collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +var_bytestring2: collections.abc.ByteString[int] diff --git a/tests/functional/g/generic_alias/generic_alias_collections.rc b/tests/functional/g/generic_alias/generic_alias_collections.rc new file mode 100644 index 0000000000..16b75eea75 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_collections.txt b/tests/functional/g/generic_alias/generic_alias_collections.txt new file mode 100644 index 0000000000..d9ce1f4432 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections.txt @@ -0,0 +1,16 @@ +unsubscriptable-object:66:0::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:67:0::Value 'collections.abc.Sized' is unsubscriptable +abstract-method:74:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:77:0:DerivedIterable:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:80:0:DerivedCollection:Method '__contains__' is abstract in class 'Container' but is not overridden +abstract-method:80:0:DerivedCollection:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:80:0:DerivedCollection:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:99:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:99:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:104:0:CustomAbstractCls2:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:104:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:106:0:CustomImplementation:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:106:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:125:9::Value 'int' is unsubscriptable +unsubscriptable-object:126:15::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:127:12::Value 'collections.abc.Sized' is unsubscriptable diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37.py b/tests/functional/g/generic_alias/generic_alias_collections_py37.py new file mode 100644 index 0000000000..eaa153cedb --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37.py @@ -0,0 +1,133 @@ +"""Test generic alias support for stdlib types (added in PY39). + +Raise [unsubscriptable-object] error for PY37 and PY38. +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +import abc +import collections +import collections.abc +import contextlib +import re + +# special +tuple[int, int] # [unsubscriptable-object] +type[int] # [unsubscriptable-object] +collections.abc.Callable[[int], str] # [unsubscriptable-object] + +# builtins +dict[int, str] # [unsubscriptable-object] +list[int] # [unsubscriptable-object] +set[int] # [unsubscriptable-object] +frozenset[int] # [unsubscriptable-object] + +# collections +collections.defaultdict[int, str] # [unsubscriptable-object] +collections.OrderedDict[int, str] # [unsubscriptable-object] +collections.ChainMap[int, str] # [unsubscriptable-object] +collections.Counter[int] # [unsubscriptable-object] +collections.deque[int] # [unsubscriptable-object] + +# collections.abc +collections.abc.Set[int] # [unsubscriptable-object] +collections.abc.Collection[int] # [unsubscriptable-object] +collections.abc.Container[int] # [unsubscriptable-object] +collections.abc.ItemsView[int, str] # [unsubscriptable-object] +collections.abc.KeysView[int] # [unsubscriptable-object] +collections.abc.Mapping[int, str] # [unsubscriptable-object] +collections.abc.MappingView[int] # [unsubscriptable-object] +collections.abc.MutableMapping[int, str] # [unsubscriptable-object] +collections.abc.MutableSequence[int] # [unsubscriptable-object] +collections.abc.MutableSet[int] # [unsubscriptable-object] +collections.abc.Sequence[int] # [unsubscriptable-object] +collections.abc.ValuesView[int] # [unsubscriptable-object] + +collections.abc.Iterable[int] # [unsubscriptable-object] +collections.abc.Iterator[int] # [unsubscriptable-object] +collections.abc.Generator[int, None, None] # [unsubscriptable-object] +collections.abc.Reversible[int] # [unsubscriptable-object] + +collections.abc.Coroutine[list[str], str, int] # [unsubscriptable-object,unsubscriptable-object] +collections.abc.AsyncGenerator[int, None] # [unsubscriptable-object] +collections.abc.AsyncIterable[int] # [unsubscriptable-object] +collections.abc.AsyncIterator[int] # [unsubscriptable-object] +collections.abc.Awaitable[int] # [unsubscriptable-object] + +# contextlib +contextlib.AbstractContextManager[int] # [unsubscriptable-object] +contextlib.AbstractAsyncContextManager[int] # [unsubscriptable-object] + +# re +re.Pattern[str] # [unsubscriptable-object] +re.Match[str] # [unsubscriptable-object] + + +# unsubscriptable types +collections.abc.Hashable +collections.abc.Sized +collections.abc.Hashable[int] # [unsubscriptable-object] +collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +collections.abc.ByteString[int] # [unsubscriptable-object] + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(collections.abc.Iterable[int]): # [unsubscriptable-object] + pass + +class DerivedCollection(collections.abc.Collection[int]): # [unsubscriptable-object] + pass + + +# No implementation required for 'builtins' and 'collections' types +class DerivedList(list[int]): # [unsubscriptable-object] + pass + +class DerivedSet(set[int]): # [unsubscriptable-object] + pass + +class DerivedOrderedDict(collections.OrderedDict[int, str]): # [unsubscriptable-object] + pass + +class DerivedListIterable(list[collections.abc.Iterable[int]]): # [unsubscriptable-object,unsubscriptable-object] + pass + + +# Multiple generic base classes +class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method] + pass + +class CustomAbstractCls1(abc.ABC): + pass +class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,unsubscriptable-object] # __len__ + pass +class CustomImplementation(CustomAbstractCls2): # [abstract-method] # __len__ + pass + + +# Type annotations +var_tuple: tuple[int, int] # [unsubscriptable-object] +var_dict: dict[int, str] # [unsubscriptable-object] +var_orderedDict: collections.OrderedDict[int, str] # [unsubscriptable-object] +var_container: collections.abc.Container[int] # [unsubscriptable-object] +var_sequence: collections.abc.Sequence[int] # [unsubscriptable-object] +var_iterable: collections.abc.Iterable[int] # [unsubscriptable-object] +var_awaitable: collections.abc.Awaitable[int] # [unsubscriptable-object] +var_contextmanager: contextlib.AbstractContextManager[int] # [unsubscriptable-object] +var_pattern: re.Pattern[int] # [unsubscriptable-object] +var_bytestring: collections.abc.ByteString +var_hashable: collections.abc.Hashable +var_sized: collections.abc.Sized + +# Type annotation with unsubscriptable type +var_int: int[int] # [unsubscriptable-object] +var_hashable2: collections.abc.Hashable[int] # [unsubscriptable-object] +var_sized2: collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +var_bytestring2: collections.abc.ByteString[int] # [unsubscriptable-object] diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37.rc b/tests/functional/g/generic_alias/generic_alias_collections_py37.rc new file mode 100644 index 0000000000..59df8e3c3b --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37.rc @@ -0,0 +1,3 @@ +[testoptions] +min_pyver=3.7 +max_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37.txt b/tests/functional/g/generic_alias/generic_alias_collections_py37.txt new file mode 100644 index 0000000000..6108073e6d --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37.txt @@ -0,0 +1,67 @@ +unsubscriptable-object:15:0::Value 'tuple' is unsubscriptable +unsubscriptable-object:16:0::Value 'type' is unsubscriptable +unsubscriptable-object:17:0::Value 'collections.abc.Callable' is unsubscriptable +unsubscriptable-object:20:0::Value 'dict' is unsubscriptable +unsubscriptable-object:21:0::Value 'list' is unsubscriptable +unsubscriptable-object:22:0::Value 'set' is unsubscriptable +unsubscriptable-object:23:0::Value 'frozenset' is unsubscriptable +unsubscriptable-object:26:0::Value 'collections.defaultdict' is unsubscriptable +unsubscriptable-object:27:0::Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:28:0::Value 'collections.ChainMap' is unsubscriptable +unsubscriptable-object:29:0::Value 'collections.Counter' is unsubscriptable +unsubscriptable-object:30:0::Value 'collections.deque' is unsubscriptable +unsubscriptable-object:33:0::Value 'collections.abc.Set' is unsubscriptable +unsubscriptable-object:34:0::Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:35:0::Value 'collections.abc.Container' is unsubscriptable +unsubscriptable-object:36:0::Value 'collections.abc.ItemsView' is unsubscriptable +unsubscriptable-object:37:0::Value 'collections.abc.KeysView' is unsubscriptable +unsubscriptable-object:38:0::Value 'collections.abc.Mapping' is unsubscriptable +unsubscriptable-object:39:0::Value 'collections.abc.MappingView' is unsubscriptable +unsubscriptable-object:40:0::Value 'collections.abc.MutableMapping' is unsubscriptable +unsubscriptable-object:41:0::Value 'collections.abc.MutableSequence' is unsubscriptable +unsubscriptable-object:42:0::Value 'collections.abc.MutableSet' is unsubscriptable +unsubscriptable-object:43:0::Value 'collections.abc.Sequence' is unsubscriptable +unsubscriptable-object:44:0::Value 'collections.abc.ValuesView' is unsubscriptable +unsubscriptable-object:46:0::Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:47:0::Value 'collections.abc.Iterator' is unsubscriptable +unsubscriptable-object:48:0::Value 'collections.abc.Generator' is unsubscriptable +unsubscriptable-object:49:0::Value 'collections.abc.Reversible' is unsubscriptable +unsubscriptable-object:51:0::Value 'collections.abc.Coroutine' is unsubscriptable +unsubscriptable-object:51:26::Value 'list' is unsubscriptable +unsubscriptable-object:52:0::Value 'collections.abc.AsyncGenerator' is unsubscriptable +unsubscriptable-object:53:0::Value 'collections.abc.AsyncIterable' is unsubscriptable +unsubscriptable-object:54:0::Value 'collections.abc.AsyncIterator' is unsubscriptable +unsubscriptable-object:55:0::Value 'collections.abc.Awaitable' is unsubscriptable +unsubscriptable-object:58:0::Value 'contextlib.AbstractContextManager' is unsubscriptable +unsubscriptable-object:59:0::Value 'contextlib.AbstractAsyncContextManager' is unsubscriptable +unsubscriptable-object:62:0::Value 're.Pattern' is unsubscriptable +unsubscriptable-object:63:0::Value 're.Match' is unsubscriptable +unsubscriptable-object:69:0::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:70:0::Value 'collections.abc.Sized' is unsubscriptable +unsubscriptable-object:73:0::Value 'collections.abc.ByteString' is unsubscriptable +abstract-method:77:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +unsubscriptable-object:80:22:DerivedIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:83:24:DerivedCollection:Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:88:18:DerivedList:Value 'list' is unsubscriptable +unsubscriptable-object:91:17:DerivedSet:Value 'set' is unsubscriptable +unsubscriptable-object:94:25:DerivedOrderedDict:Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:97:31:DerivedListIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:97:26:DerivedListIterable:Value 'list' is unsubscriptable +abstract-method:102:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:102:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:107:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:107:48:CustomAbstractCls2:Value 'collections.abc.Iterable' is unsubscriptable +abstract-method:109:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:114:11::Value 'tuple' is unsubscriptable +unsubscriptable-object:115:10::Value 'dict' is unsubscriptable +unsubscriptable-object:116:17::Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:117:15::Value 'collections.abc.Container' is unsubscriptable +unsubscriptable-object:118:14::Value 'collections.abc.Sequence' is unsubscriptable +unsubscriptable-object:119:14::Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:120:15::Value 'collections.abc.Awaitable' is unsubscriptable +unsubscriptable-object:121:20::Value 'contextlib.AbstractContextManager' is unsubscriptable +unsubscriptable-object:122:13::Value 're.Pattern' is unsubscriptable +unsubscriptable-object:128:9::Value 'int' is unsubscriptable +unsubscriptable-object:129:15::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:130:12::Value 'collections.abc.Sized' is unsubscriptable +unsubscriptable-object:133:17::Value 'collections.abc.ByteString' is unsubscriptable diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.py b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.py new file mode 100644 index 0000000000..5319f13b62 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.py @@ -0,0 +1,135 @@ +"""Test generic alias support for stdlib types (added in PY39). + +Raise [unsubscriptable-object] error for PY37 and PY38. +Make sure `import typing` doesn't change anything. +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement,unused-import +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +import abc +import collections +import collections.abc +import contextlib +import re +import typing + +# special +tuple[int, int] # [unsubscriptable-object] +type[int] # [unsubscriptable-object] +collections.abc.Callable[[int], str] # [unsubscriptable-object] + +# builtins +dict[int, str] # [unsubscriptable-object] +list[int] # [unsubscriptable-object] +set[int] # [unsubscriptable-object] +frozenset[int] # [unsubscriptable-object] + +# collections +collections.defaultdict[int, str] # [unsubscriptable-object] +collections.OrderedDict[int, str] # [unsubscriptable-object] +collections.ChainMap[int, str] # [unsubscriptable-object] +collections.Counter[int] # [unsubscriptable-object] +collections.deque[int] # [unsubscriptable-object] + +# collections.abc +collections.abc.Set[int] # [unsubscriptable-object] +collections.abc.Collection[int] # [unsubscriptable-object] +collections.abc.Container[int] # [unsubscriptable-object] +collections.abc.ItemsView[int, str] # [unsubscriptable-object] +collections.abc.KeysView[int] # [unsubscriptable-object] +collections.abc.Mapping[int, str] # [unsubscriptable-object] +collections.abc.MappingView[int] # [unsubscriptable-object] +collections.abc.MutableMapping[int, str] # [unsubscriptable-object] +collections.abc.MutableSequence[int] # [unsubscriptable-object] +collections.abc.MutableSet[int] # [unsubscriptable-object] +collections.abc.Sequence[int] # [unsubscriptable-object] +collections.abc.ValuesView[int] # [unsubscriptable-object] + +collections.abc.Iterable[int] # [unsubscriptable-object] +collections.abc.Iterator[int] # [unsubscriptable-object] +collections.abc.Generator[int, None, None] # [unsubscriptable-object] +collections.abc.Reversible[int] # [unsubscriptable-object] + +collections.abc.Coroutine[list[str], str, int] # [unsubscriptable-object,unsubscriptable-object] +collections.abc.AsyncGenerator[int, None] # [unsubscriptable-object] +collections.abc.AsyncIterable[int] # [unsubscriptable-object] +collections.abc.AsyncIterator[int] # [unsubscriptable-object] +collections.abc.Awaitable[int] # [unsubscriptable-object] + +# contextlib +contextlib.AbstractContextManager[int] # [unsubscriptable-object] +contextlib.AbstractAsyncContextManager[int] # [unsubscriptable-object] + +# re +re.Pattern[str] # [unsubscriptable-object] +re.Match[str] # [unsubscriptable-object] + + +# unsubscriptable types +collections.abc.Hashable +collections.abc.Sized +collections.abc.Hashable[int] # [unsubscriptable-object] +collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +collections.abc.ByteString[int] # [unsubscriptable-object] + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(collections.abc.Iterable[int]): # [unsubscriptable-object] + pass + +class DerivedCollection(collections.abc.Collection[int]): # [unsubscriptable-object] + pass + + +# No implementation required for 'builtins' and 'collections' types +class DerivedList(list[int]): # [unsubscriptable-object] + pass + +class DerivedSet(set[int]): # [unsubscriptable-object] + pass + +class DerivedOrderedDict(collections.OrderedDict[int, str]): # [unsubscriptable-object] + pass + +class DerivedListIterable(list[collections.abc.Iterable[int]]): # [unsubscriptable-object,unsubscriptable-object] + pass + + +# Multiple generic base classes +class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method] + pass + +class CustomAbstractCls1(abc.ABC): + pass +class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,unsubscriptable-object] # __len__ + pass +class CustomImplementation(CustomAbstractCls2): # [abstract-method] # __len__ + pass + + +# Type annotations +var_tuple: tuple[int, int] # [unsubscriptable-object] +var_dict: dict[int, str] # [unsubscriptable-object] +var_orderedDict: collections.OrderedDict[int, str] # [unsubscriptable-object] +var_container: collections.abc.Container[int] # [unsubscriptable-object] +var_sequence: collections.abc.Sequence[int] # [unsubscriptable-object] +var_iterable: collections.abc.Iterable[int] # [unsubscriptable-object] +var_awaitable: collections.abc.Awaitable[int] # [unsubscriptable-object] +var_contextmanager: contextlib.AbstractContextManager[int] # [unsubscriptable-object] +var_pattern: re.Pattern[int] # [unsubscriptable-object] +var_bytestring: collections.abc.ByteString +var_hashable: collections.abc.Hashable +var_sized: collections.abc.Sized + +# Type annotation with unsubscriptable type +var_int: int[int] # [unsubscriptable-object] +var_hashable2: collections.abc.Hashable[int] # [unsubscriptable-object] +var_sized2: collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +var_bytestring2: collections.abc.ByteString[int] # [unsubscriptable-object] diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.rc b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.rc new file mode 100644 index 0000000000..59df8e3c3b --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.rc @@ -0,0 +1,3 @@ +[testoptions] +min_pyver=3.7 +max_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.txt b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.txt new file mode 100644 index 0000000000..ad7bc4a1bd --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_collections_py37_with_typing.txt @@ -0,0 +1,67 @@ +unsubscriptable-object:17:0::Value 'tuple' is unsubscriptable +unsubscriptable-object:18:0::Value 'type' is unsubscriptable +unsubscriptable-object:19:0::Value 'collections.abc.Callable' is unsubscriptable +unsubscriptable-object:22:0::Value 'dict' is unsubscriptable +unsubscriptable-object:23:0::Value 'list' is unsubscriptable +unsubscriptable-object:24:0::Value 'set' is unsubscriptable +unsubscriptable-object:25:0::Value 'frozenset' is unsubscriptable +unsubscriptable-object:28:0::Value 'collections.defaultdict' is unsubscriptable +unsubscriptable-object:29:0::Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:30:0::Value 'collections.ChainMap' is unsubscriptable +unsubscriptable-object:31:0::Value 'collections.Counter' is unsubscriptable +unsubscriptable-object:32:0::Value 'collections.deque' is unsubscriptable +unsubscriptable-object:35:0::Value 'collections.abc.Set' is unsubscriptable +unsubscriptable-object:36:0::Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:37:0::Value 'collections.abc.Container' is unsubscriptable +unsubscriptable-object:38:0::Value 'collections.abc.ItemsView' is unsubscriptable +unsubscriptable-object:39:0::Value 'collections.abc.KeysView' is unsubscriptable +unsubscriptable-object:40:0::Value 'collections.abc.Mapping' is unsubscriptable +unsubscriptable-object:41:0::Value 'collections.abc.MappingView' is unsubscriptable +unsubscriptable-object:42:0::Value 'collections.abc.MutableMapping' is unsubscriptable +unsubscriptable-object:43:0::Value 'collections.abc.MutableSequence' is unsubscriptable +unsubscriptable-object:44:0::Value 'collections.abc.MutableSet' is unsubscriptable +unsubscriptable-object:45:0::Value 'collections.abc.Sequence' is unsubscriptable +unsubscriptable-object:46:0::Value 'collections.abc.ValuesView' is unsubscriptable +unsubscriptable-object:48:0::Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:49:0::Value 'collections.abc.Iterator' is unsubscriptable +unsubscriptable-object:50:0::Value 'collections.abc.Generator' is unsubscriptable +unsubscriptable-object:51:0::Value 'collections.abc.Reversible' is unsubscriptable +unsubscriptable-object:53:0::Value 'collections.abc.Coroutine' is unsubscriptable +unsubscriptable-object:53:26::Value 'list' is unsubscriptable +unsubscriptable-object:54:0::Value 'collections.abc.AsyncGenerator' is unsubscriptable +unsubscriptable-object:55:0::Value 'collections.abc.AsyncIterable' is unsubscriptable +unsubscriptable-object:56:0::Value 'collections.abc.AsyncIterator' is unsubscriptable +unsubscriptable-object:57:0::Value 'collections.abc.Awaitable' is unsubscriptable +unsubscriptable-object:60:0::Value 'contextlib.AbstractContextManager' is unsubscriptable +unsubscriptable-object:61:0::Value 'contextlib.AbstractAsyncContextManager' is unsubscriptable +unsubscriptable-object:64:0::Value 're.Pattern' is unsubscriptable +unsubscriptable-object:65:0::Value 're.Match' is unsubscriptable +unsubscriptable-object:71:0::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:72:0::Value 'collections.abc.Sized' is unsubscriptable +unsubscriptable-object:75:0::Value 'collections.abc.ByteString' is unsubscriptable +abstract-method:79:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +unsubscriptable-object:82:22:DerivedIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:85:24:DerivedCollection:Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:90:18:DerivedList:Value 'list' is unsubscriptable +unsubscriptable-object:93:17:DerivedSet:Value 'set' is unsubscriptable +unsubscriptable-object:96:25:DerivedOrderedDict:Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:99:31:DerivedListIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:99:26:DerivedListIterable:Value 'list' is unsubscriptable +abstract-method:104:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:104:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:109:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:109:48:CustomAbstractCls2:Value 'collections.abc.Iterable' is unsubscriptable +abstract-method:111:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:116:11::Value 'tuple' is unsubscriptable +unsubscriptable-object:117:10::Value 'dict' is unsubscriptable +unsubscriptable-object:118:17::Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:119:15::Value 'collections.abc.Container' is unsubscriptable +unsubscriptable-object:120:14::Value 'collections.abc.Sequence' is unsubscriptable +unsubscriptable-object:121:14::Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:122:15::Value 'collections.abc.Awaitable' is unsubscriptable +unsubscriptable-object:123:20::Value 'contextlib.AbstractContextManager' is unsubscriptable +unsubscriptable-object:124:13::Value 're.Pattern' is unsubscriptable +unsubscriptable-object:130:9::Value 'int' is unsubscriptable +unsubscriptable-object:131:15::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:132:12::Value 'collections.abc.Sized' is unsubscriptable +unsubscriptable-object:135:17::Value 'collections.abc.ByteString' is unsubscriptable diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py37.py b/tests/functional/g/generic_alias/generic_alias_mixed_py37.py new file mode 100644 index 0000000000..cb7a4d0f43 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py37.py @@ -0,0 +1,41 @@ +"""Test generic alias support with mix of typing.py and stdlib types. + +Possible with postponed evaluation enabled, starting with PY37. +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +from __future__ import annotations + +import collections +import collections.abc +import contextlib +import re +import typing + +# Type annotations +var_orderedDict: collections.OrderedDict[int, str] +var_container: collections.abc.Container[int] +var_sequence: collections.abc.Sequence[int] +var_iterable: collections.abc.Iterable[int] +var_awaitable: collections.abc.Awaitable[int] +var_pattern: re.Pattern[int] +var_bytestring: collections.abc.ByteString +var_hashable: collections.abc.Hashable +var_ContextManager: contextlib.AbstractContextManager[int] + + +# No implementation required for 'builtins' +class DerivedListIterable(typing.List[typing.Iterable[int]]): + pass + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(typing.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(typing.Iterable[int]): # [abstract-method] # __iter__ + pass + +class DerivedCollection(typing.Collection[int]): # [abstract-method,abstract-method,abstract-method] # __contains__, __iter__, __len__ + pass diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py37.rc b/tests/functional/g/generic_alias/generic_alias_mixed_py37.rc new file mode 100644 index 0000000000..59df8e3c3b --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py37.rc @@ -0,0 +1,3 @@ +[testoptions] +min_pyver=3.7 +max_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py37.txt b/tests/functional/g/generic_alias/generic_alias_mixed_py37.txt new file mode 100644 index 0000000000..14d784ea0d --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py37.txt @@ -0,0 +1,5 @@ +abstract-method:34:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:37:0:DerivedIterable:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:40:0:DerivedCollection:Method '__contains__' is abstract in class 'Container' but is not overridden +abstract-method:40:0:DerivedCollection:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:40:0:DerivedCollection:Method '__len__' is abstract in class 'Sized' but is not overridden diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py39.py b/tests/functional/g/generic_alias/generic_alias_mixed_py39.py new file mode 100644 index 0000000000..9c6e63e661 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py39.py @@ -0,0 +1,36 @@ +"""Test generic alias support with mix of typing.py and stdlib types (PY39+).""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +import collections +import collections.abc +import contextlib +import re +import typing + +# Type annotations +var_orderedDict: collections.OrderedDict[int, str] +var_container: collections.abc.Container[int] +var_sequence: collections.abc.Sequence[int] +var_iterable: collections.abc.Iterable[int] +var_awaitable: collections.abc.Awaitable[int] +var_pattern: re.Pattern[int] +var_bytestring: collections.abc.ByteString +var_hashable: collections.abc.Hashable +var_ContextManager: contextlib.AbstractContextManager[int] + + +# No implementation required for 'builtins' +class DerivedListIterable(typing.List[typing.Iterable[int]]): + pass + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(typing.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(typing.Iterable[int]): # [abstract-method] # __iter__ + pass + +class DerivedCollection(typing.Collection[int]): # [abstract-method,abstract-method,abstract-method] # __contains__, __iter__, __len__ + pass diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py39.rc b/tests/functional/g/generic_alias/generic_alias_mixed_py39.rc new file mode 100644 index 0000000000..16b75eea75 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py39.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_mixed_py39.txt b/tests/functional/g/generic_alias/generic_alias_mixed_py39.txt new file mode 100644 index 0000000000..2247c127f2 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_mixed_py39.txt @@ -0,0 +1,5 @@ +abstract-method:29:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:32:0:DerivedIterable:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:35:0:DerivedCollection:Method '__contains__' is abstract in class 'Container' but is not overridden +abstract-method:35:0:DerivedCollection:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:35:0:DerivedCollection:Method '__len__' is abstract in class 'Sized' but is not overridden diff --git a/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.py b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.py new file mode 100644 index 0000000000..5ded73b3dd --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.py @@ -0,0 +1,179 @@ +"""Test generic alias support for stdlib types (added in PY39). + +In type annotation context, they can be used with postponed evaluation enabled, +starting with PY37. +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +from __future__ import annotations + +import abc +import collections +import collections.abc +import contextlib +import re + + +# ----- unsubscriptable (even with postponed evaluation) ----- +# special +tuple[int, int] # [unsubscriptable-object] +type[int] # [unsubscriptable-object] +collections.abc.Callable[[int], str] # [unsubscriptable-object] + +# builtins +dict[int, str] # [unsubscriptable-object] +list[int] # [unsubscriptable-object] +set[int] # [unsubscriptable-object] +frozenset[int] # [unsubscriptable-object] + +# collections +collections.defaultdict[int, str] # [unsubscriptable-object] +collections.OrderedDict[int, str] # [unsubscriptable-object] +collections.ChainMap[int, str] # [unsubscriptable-object] +collections.Counter[int] # [unsubscriptable-object] +collections.deque[int] # [unsubscriptable-object] + +# collections.abc +collections.abc.Set[int] # [unsubscriptable-object] +collections.abc.Collection[int] # [unsubscriptable-object] +collections.abc.Container[int] # [unsubscriptable-object] +collections.abc.ItemsView[int, str] # [unsubscriptable-object] +collections.abc.KeysView[int] # [unsubscriptable-object] +collections.abc.Mapping[int, str] # [unsubscriptable-object] +collections.abc.MappingView[int] # [unsubscriptable-object] +collections.abc.MutableMapping[int, str] # [unsubscriptable-object] +collections.abc.MutableSequence[int] # [unsubscriptable-object] +collections.abc.MutableSet[int] # [unsubscriptable-object] +collections.abc.Sequence[int] # [unsubscriptable-object] +collections.abc.ValuesView[int] # [unsubscriptable-object] + +collections.abc.Iterable[int] # [unsubscriptable-object] +collections.abc.Iterator[int] # [unsubscriptable-object] +collections.abc.Generator[int, None, None] # [unsubscriptable-object] +collections.abc.Reversible[int] # [unsubscriptable-object] + +collections.abc.Coroutine[list[str], str, int] # [unsubscriptable-object,unsubscriptable-object] +collections.abc.AsyncGenerator[int, None] # [unsubscriptable-object] +collections.abc.AsyncIterable[int] # [unsubscriptable-object] +collections.abc.AsyncIterator[int] # [unsubscriptable-object] +collections.abc.Awaitable[int] # [unsubscriptable-object] + +# contextlib +contextlib.AbstractContextManager[int] # [unsubscriptable-object] +contextlib.AbstractAsyncContextManager[int] # [unsubscriptable-object] + +# re +re.Pattern[str] # [unsubscriptable-object] +re.Match[str] # [unsubscriptable-object] + + +# unsubscriptable types +collections.abc.Hashable +collections.abc.Sized +collections.abc.Hashable[int] # [unsubscriptable-object] +collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +collections.abc.ByteString[int] # [unsubscriptable-object] + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(collections.abc.Iterable[int]): # [unsubscriptable-object] + pass + +class DerivedCollection(collections.abc.Collection[int]): # [unsubscriptable-object] + pass + + +# No implementation required for 'builtins' and 'collections' types +class DerivedList(list[int]): # [unsubscriptable-object] + pass + +class DerivedSet(set[int]): # [unsubscriptable-object] + pass + +class DerivedOrderedDict(collections.OrderedDict[int, str]): # [unsubscriptable-object] + pass + +class DerivedListIterable(list[collections.abc.Iterable[int]]): # [unsubscriptable-object,unsubscriptable-object] + pass + + +# Multiple generic base classes +class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method] + pass + +class CustomAbstractCls1(abc.ABC): + pass +class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,unsubscriptable-object] # __len__ + pass +class CustomImplementation(CustomAbstractCls2): # [abstract-method] # __len__ + pass + + + +# ----- subscriptable (with postponed evaluation) ----- +# special +var_tuple: tuple[int, int] +var_type: type[int] +var_callable: collections.abc.Callable[[int], str] + +# builtins +var_dict: dict[int, str] +var_list: list[int] +var_set: set[int] +var_frozenset: frozenset[int] + +# collections +var_defaultdict: collections.defaultdict[int, str] +var_OrderedDict: collections.OrderedDict[int, str] +var_ChainMap: collections.ChainMap[int, str] +var_Counter: collections.Counter[int] +var_deque: collections.deque[int] + +# collections.abc +var_abc_set: collections.abc.Set[int] +var_abc_collection: collections.abc.Collection[int] +var_abc_container: collections.abc.Container[int] +var_abc_ItemsView: collections.abc.ItemsView[int, str] +var_abc_KeysView: collections.abc.KeysView[int] +var_abc_Mapping: collections.abc.Mapping[int, str] +var_abc_MappingView: collections.abc.MappingView[int] +var_abc_MutableMapping: collections.abc.MutableMapping[int, str] +var_abc_MutableSequence: collections.abc.MutableSequence[int] +var_abc_MutableSet: collections.abc.MutableSet[int] +var_abc_Sequence: collections.abc.Sequence[int] +var_abc_ValuesView: collections.abc.ValuesView[int] + +var_abc_Iterable: collections.abc.Iterable[int] +var_abc_Iterator: collections.abc.Iterator[int] +var_abc_Generator: collections.abc.Generator[int, None, None] +var_abc_Reversible: collections.abc.Reversible[int] + +var_abc_Coroutine: collections.abc.Coroutine[list[str], str, int] +var_abc_AsyncGenerator: collections.abc.AsyncGenerator[int, None] +var_abc_AsyncIterable: collections.abc.AsyncIterable[int] +var_abc_AsyncIterator: collections.abc.AsyncIterator[int] +var_abc_Awaitable: collections.abc.Awaitable[int] + +# contextlib +var_ContextManager: contextlib.AbstractContextManager[int] +var_AsyncContextManager: contextlib.AbstractAsyncContextManager[int] + +# re +var_re_Pattern: re.Pattern[str] +var_re_Match: re.Match[str] + + +# unsubscriptable types +var_abc_Hashable: collections.abc.Hashable +var_abc_Sized: collections.abc.Sized +var_abc_Hashable2: collections.abc.Hashable[int] # [unsubscriptable-object] +var_abc_Sized2: collections.abc.Sized[int] # [unsubscriptable-object] + +# subscriptable with Python 3.9 +var_abc_ByteString: collections.abc.ByteString[int] diff --git a/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.rc b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.rc new file mode 100644 index 0000000000..59df8e3c3b --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.rc @@ -0,0 +1,3 @@ +[testoptions] +min_pyver=3.7 +max_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.txt b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.txt new file mode 100644 index 0000000000..5bfcb48458 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_postponed_evaluation_py37.txt @@ -0,0 +1,56 @@ +unsubscriptable-object:20:0::Value 'tuple' is unsubscriptable +unsubscriptable-object:21:0::Value 'type' is unsubscriptable +unsubscriptable-object:22:0::Value 'collections.abc.Callable' is unsubscriptable +unsubscriptable-object:25:0::Value 'dict' is unsubscriptable +unsubscriptable-object:26:0::Value 'list' is unsubscriptable +unsubscriptable-object:27:0::Value 'set' is unsubscriptable +unsubscriptable-object:28:0::Value 'frozenset' is unsubscriptable +unsubscriptable-object:31:0::Value 'collections.defaultdict' is unsubscriptable +unsubscriptable-object:32:0::Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:33:0::Value 'collections.ChainMap' is unsubscriptable +unsubscriptable-object:34:0::Value 'collections.Counter' is unsubscriptable +unsubscriptable-object:35:0::Value 'collections.deque' is unsubscriptable +unsubscriptable-object:38:0::Value 'collections.abc.Set' is unsubscriptable +unsubscriptable-object:39:0::Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:40:0::Value 'collections.abc.Container' is unsubscriptable +unsubscriptable-object:41:0::Value 'collections.abc.ItemsView' is unsubscriptable +unsubscriptable-object:42:0::Value 'collections.abc.KeysView' is unsubscriptable +unsubscriptable-object:43:0::Value 'collections.abc.Mapping' is unsubscriptable +unsubscriptable-object:44:0::Value 'collections.abc.MappingView' is unsubscriptable +unsubscriptable-object:45:0::Value 'collections.abc.MutableMapping' is unsubscriptable +unsubscriptable-object:46:0::Value 'collections.abc.MutableSequence' is unsubscriptable +unsubscriptable-object:47:0::Value 'collections.abc.MutableSet' is unsubscriptable +unsubscriptable-object:48:0::Value 'collections.abc.Sequence' is unsubscriptable +unsubscriptable-object:49:0::Value 'collections.abc.ValuesView' is unsubscriptable +unsubscriptable-object:51:0::Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:52:0::Value 'collections.abc.Iterator' is unsubscriptable +unsubscriptable-object:53:0::Value 'collections.abc.Generator' is unsubscriptable +unsubscriptable-object:54:0::Value 'collections.abc.Reversible' is unsubscriptable +unsubscriptable-object:56:0::Value 'collections.abc.Coroutine' is unsubscriptable +unsubscriptable-object:56:26::Value 'list' is unsubscriptable +unsubscriptable-object:57:0::Value 'collections.abc.AsyncGenerator' is unsubscriptable +unsubscriptable-object:58:0::Value 'collections.abc.AsyncIterable' is unsubscriptable +unsubscriptable-object:59:0::Value 'collections.abc.AsyncIterator' is unsubscriptable +unsubscriptable-object:60:0::Value 'collections.abc.Awaitable' is unsubscriptable +unsubscriptable-object:63:0::Value 'contextlib.AbstractContextManager' is unsubscriptable +unsubscriptable-object:64:0::Value 'contextlib.AbstractAsyncContextManager' is unsubscriptable +unsubscriptable-object:67:0::Value 're.Pattern' is unsubscriptable +unsubscriptable-object:68:0::Value 're.Match' is unsubscriptable +unsubscriptable-object:74:0::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:75:0::Value 'collections.abc.Sized' is unsubscriptable +unsubscriptable-object:78:0::Value 'collections.abc.ByteString' is unsubscriptable +abstract-method:82:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +unsubscriptable-object:85:22:DerivedIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:88:24:DerivedCollection:Value 'collections.abc.Collection' is unsubscriptable +unsubscriptable-object:93:18:DerivedList:Value 'list' is unsubscriptable +unsubscriptable-object:96:17:DerivedSet:Value 'set' is unsubscriptable +unsubscriptable-object:99:25:DerivedOrderedDict:Value 'collections.OrderedDict' is unsubscriptable +unsubscriptable-object:102:31:DerivedListIterable:Value 'collections.abc.Iterable' is unsubscriptable +unsubscriptable-object:102:26:DerivedListIterable:Value 'list' is unsubscriptable +abstract-method:107:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:107:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:112:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:112:48:CustomAbstractCls2:Value 'collections.abc.Iterable' is unsubscriptable +abstract-method:114:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden +unsubscriptable-object:175:19::Value 'collections.abc.Hashable' is unsubscriptable +unsubscriptable-object:176:16::Value 'collections.abc.Sized' is unsubscriptable diff --git a/tests/functional/g/generic_alias/generic_alias_related.py b/tests/functional/g/generic_alias/generic_alias_related.py new file mode 100644 index 0000000000..20b074bae5 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related.py @@ -0,0 +1,54 @@ +"""Test related function for generic alias. + +Any solution should not change the behavior of +- `__getitem__` +- `__class_getitem__` +- `metaclass=ABCMeta` +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement,expression-not-assigned +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +from abc import ABCMeta, abstractmethod +import typing + + +class ClsUnsubscriptable: + def __init__(self): + pass + +class ClsGetItem: + def __init__(self): + self.var = [1, 2, 3, 4] + def __getitem__(self, item): + return self.var[item] + +class ClsClassGetItem: + def __init__(self): + pass + __class_getitem__ = lambda cls, x: None + +class ClsList(typing.List): + pass + + +ClsUnsubscriptable()[1] # [unsubscriptable-object] +ClsUnsubscriptable[int] # [unsubscriptable-object] + +ClsGetItem()[1] +ClsGetItem[int] # [unsubscriptable-object] + +ClsClassGetItem()[1] # [unsubscriptable-object] +ClsClassGetItem[int] + +# subscriptable because of inheritance +ClsList([0, 1, 2])[1] +ClsList[int] + + +class ClsAbstract(metaclass=ABCMeta): + @abstractmethod + def abstract_method(self): + pass + +class Derived(ClsAbstract): # [abstract-method] + pass diff --git a/tests/functional/g/generic_alias/generic_alias_related.rc b/tests/functional/g/generic_alias/generic_alias_related.rc new file mode 100644 index 0000000000..a17bb22daf --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.7 diff --git a/tests/functional/g/generic_alias/generic_alias_related.txt b/tests/functional/g/generic_alias/generic_alias_related.txt new file mode 100644 index 0000000000..b10f66f304 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related.txt @@ -0,0 +1,5 @@ +unsubscriptable-object:34:0::Value 'ClsUnsubscriptable()' is unsubscriptable +unsubscriptable-object:35:0::Value 'ClsUnsubscriptable' is unsubscriptable +unsubscriptable-object:38:0::Value 'ClsGetItem' is unsubscriptable +unsubscriptable-object:40:0::Value 'ClsClassGetItem()' is unsubscriptable +abstract-method:53:0:Derived:Method 'abstract_method' is abstract in class 'ClsAbstract' but is not overridden diff --git a/tests/functional/g/generic_alias/generic_alias_related_py39.py b/tests/functional/g/generic_alias/generic_alias_related_py39.py new file mode 100644 index 0000000000..14445a043f --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related_py39.py @@ -0,0 +1,56 @@ +"""Test related function for generic alias. + +Any solution should not change the behavior of +- `__getitem__` +- `__class_getitem__` +- `metaclass=ABCMeta` +""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement,expression-not-assigned +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +from abc import ABCMeta, abstractmethod +import typing + +GenericAlias = type(list[int]) + + +class ClsUnsubscriptable: + def __init__(self): + pass + +class ClsGetItem: + def __init__(self): + self.var = [1, 2, 3, 4] + def __getitem__(self, item): + return self.var[item] + +class ClsClassGetItem: + def __init__(self): + pass + __class_getitem__ = classmethod(GenericAlias) + +class ClsList(typing.List): + pass + + +ClsUnsubscriptable()[1] # [unsubscriptable-object] +ClsUnsubscriptable[int] # [unsubscriptable-object] + +ClsGetItem()[1] +ClsGetItem[int] # [unsubscriptable-object] + +ClsClassGetItem()[1] # [unsubscriptable-object] +ClsClassGetItem[int] + +# subscriptable because of inheritance +ClsList([0, 1, 2])[1] +ClsList[int] + + +class ClsAbstract(metaclass=ABCMeta): + @abstractmethod + def abstract_method(self): + pass + +class Derived(ClsAbstract): # [abstract-method] + pass diff --git a/tests/functional/g/generic_alias/generic_alias_related_py39.rc b/tests/functional/g/generic_alias/generic_alias_related_py39.rc new file mode 100644 index 0000000000..16b75eea75 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related_py39.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.9 diff --git a/tests/functional/g/generic_alias/generic_alias_related_py39.txt b/tests/functional/g/generic_alias/generic_alias_related_py39.txt new file mode 100644 index 0000000000..be51879d61 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_related_py39.txt @@ -0,0 +1,5 @@ +unsubscriptable-object:36:0::Value 'ClsUnsubscriptable()' is unsubscriptable +unsubscriptable-object:37:0::Value 'ClsUnsubscriptable' is unsubscriptable +unsubscriptable-object:40:0::Value 'ClsGetItem' is unsubscriptable +unsubscriptable-object:42:0::Value 'ClsClassGetItem()' is unsubscriptable +abstract-method:55:0:Derived:Method 'abstract_method' is abstract in class 'ClsAbstract' but is not overridden diff --git a/tests/functional/g/generic_alias/generic_alias_side_effects.py b/tests/functional/g/generic_alias/generic_alias_side_effects.py new file mode 100644 index 0000000000..64b3d9810c --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_side_effects.py @@ -0,0 +1,78 @@ +# pylint: disable=missing-docstring,invalid-name,line-too-long,too-few-public-methods +import typing +import collections +from typing import Generic, TypeVar + + +# tests/functional/a/assigning_non_slot.py +TYPE = TypeVar('TYPE') + +class Cls(Generic[TYPE]): + """ Simple class with slots """ + __slots__ = ['value'] + + def __init__(self, value): + self.value = value + + +# tests/functional/d/dangerous_default_value_py30.py +def function4(value=set()): # [dangerous-default-value] + """set is mutable and dangerous.""" + return value + +def function5(value=frozenset()): + """frozenset is immutable and safe.""" + return value + +def function7(value=dict()): # [dangerous-default-value] + """dict is mutable and dangerous.""" + return value + +def function8(value=list()): # [dangerous-default-value] + """list is mutable and dangerous.""" + return value + +def function17(value=collections.deque()): # [dangerous-default-value] + """mutable, dangerous""" + return value + +def function18(value=collections.ChainMap()): # [dangerous-default-value] + """mutable, dangerous""" + return value + +def function19(value=collections.Counter()): # [dangerous-default-value] + """mutable, dangerous""" + return value + +def function20(value=collections.OrderedDict()): # [dangerous-default-value] + """mutable, dangerous""" + return value + +def function21(value=collections.defaultdict()): # [dangerous-default-value] + """mutable, dangerous""" + return value + + +# tests/functional/p/protocol_classes.py (min py38) +T2 = typing.TypeVar("T2") + +class HasherGeneric(typing.Protocol[T2]): + """A hashing algorithm, e.g. :func:`hashlib.sha256`.""" + def update(self, blob: bytes): + ... + def digest(self) -> bytes: + ... + + +# tests/functional/r/regression/regression_2443_duplicate_bases.py +IN = TypeVar('IN', contravariant=True) +OUT = TypeVar('OUT', covariant=True) + +class ConsumingMixin(Generic[IN]): + pass + +class ProducingMixin(Generic[OUT]): + pass + +class StreamingMixin(Generic[IN, OUT], ConsumingMixin[IN], ProducingMixin[OUT]): + pass diff --git a/tests/functional/g/generic_alias/generic_alias_side_effects.rc b/tests/functional/g/generic_alias/generic_alias_side_effects.rc new file mode 100644 index 0000000000..85fc502b37 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_side_effects.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.8 diff --git a/tests/functional/g/generic_alias/generic_alias_side_effects.txt b/tests/functional/g/generic_alias/generic_alias_side_effects.txt new file mode 100644 index 0000000000..bbd64c12ae --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_side_effects.txt @@ -0,0 +1,8 @@ +dangerous-default-value:19:0:function4:Dangerous default value set() (builtins.set) as argument +dangerous-default-value:27:0:function7:Dangerous default value dict() (builtins.dict) as argument +dangerous-default-value:31:0:function8:Dangerous default value list() (builtins.list) as argument +dangerous-default-value:35:0:function17:Dangerous default value deque() (collections.deque) as argument +dangerous-default-value:39:0:function18:Dangerous default value ChainMap() (collections.ChainMap) as argument +dangerous-default-value:43:0:function19:Dangerous default value Counter() (collections.Counter) as argument +dangerous-default-value:47:0:function20:Dangerous default value OrderedDict() (collections.OrderedDict) as argument +dangerous-default-value:51:0:function21:Dangerous default value defaultdict() (collections.defaultdict) as argument diff --git a/tests/functional/g/generic_alias/generic_alias_typing.py b/tests/functional/g/generic_alias/generic_alias_typing.py new file mode 100644 index 0000000000..2ce05deaf0 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_typing.py @@ -0,0 +1,141 @@ +"""Test generic alias support for typing.py types.""" +# flake8: noqa +# pylint: disable=missing-docstring,pointless-statement +# pylint: disable=too-few-public-methods,multiple-statements,line-too-long +import abc +import typing + +# special +typing.Tuple[int, int] +typing.Type[int] +typing.Callable[[int], str] + +# builtins +typing.Dict[int, str] +typing.List[int] +typing.Set[int] +typing.FrozenSet[int] + +# collections +typing.DefaultDict[int, str] +typing.OrderedDict[int, str] +typing.ChainMap[int, str] +typing.Counter[int] +typing.Deque[int] + +# collections.abc +typing.AbstractSet[int] +typing.Collection[int] +typing.Container[int] +typing.ItemsView[int, str] +typing.KeysView[int] +typing.Mapping[int, str] +typing.MappingView[int] +typing.MutableMapping[int, str] +typing.MutableSequence[int] +typing.MutableSet[int] +typing.Sequence[int] +typing.ValuesView[int] + +typing.Iterable[int] +typing.Iterator[int] +typing.Generator[int, None, None] +typing.Reversible[int] + +typing.Coroutine[typing.List[str], str, int] +typing.AsyncGenerator[int, None] +typing.AsyncIterable[int] +typing.AsyncIterator[int] +typing.Awaitable[int] + +# contextlib +typing.ContextManager[int] +typing.AsyncContextManager[int] + +# re +typing.Pattern[str] +typing.Match[str] +typing.re.Pattern[str] +typing.re.Match[str] + + +# unsubscriptable types +typing.ByteString +typing.Hashable +typing.Sized +typing.ByteString[int] # [unsubscriptable-object] +typing.Hashable[int] # [unsubscriptable-object] +typing.Sized[int] # [unsubscriptable-object] + + +# Missing implementation for 'collections.abc' derived classes +class DerivedHashable(typing.Hashable): # [abstract-method] # __hash__ + pass + +class DerivedIterable(typing.Iterable[int]): # [abstract-method] # __iter__ + pass + +class DerivedCollection(typing.Collection[int]): # [abstract-method,abstract-method,abstract-method] # __contains__, __iter__, __len__ + pass + + +# No implementation required for 'builtins' and 'collections' types +class DerivedList(typing.List[int]): + def func(self): + return self.__iter__() + +class DerivedSet(typing.Set[int]): + def func(self): + return self.add(2) + +class DerivedOrderedDict(typing.OrderedDict[int, str]): + def func(self): + return self.items() + +class DerivedListIterable(typing.List[typing.Iterable[int]]): + pass + + +# Multiple generic base classes +class DerivedMultiple(typing.Sized, typing.Hashable): # [abstract-method,abstract-method] + pass + +class CustomAbstractCls1(abc.ABC): + pass +class CustomAbstractCls2(typing.Sized, typing.Iterable[CustomAbstractCls1]): # [abstract-method,abstract-method] # __iter__, __len__ + pass +class CustomImplementation(CustomAbstractCls2): # [abstract-method,abstract-method] # __iter__, __len__ + pass + + +# Inheritance without generic +class DerivedList2(typing.List): + pass + +class DerivedOrderedDict2(typing.OrderedDict): + pass + +class DerivedIterable2(typing.Iterable): # [abstract-method] # __iter__ + pass + + +# Type annotations +var_tuple: typing.Tuple[int, int] +var_dict: typing.Dict[int, str] +var_orderedDict: typing.OrderedDict[int, str] +var_container: typing.Container[int] +var_sequence: typing.Sequence[int] +var_iterable: typing.Iterable[int] +var_awaitable: typing.Awaitable[int] +var_contextmanager: typing.ContextManager[int] +var_pattern: typing.Pattern[int] +var_pattern2: typing.re.Pattern[int] +var_bytestring: typing.ByteString +var_hashable: typing.Hashable +var_sized: typing.Sized + +# Type annotation with unsubscriptable type +var_int: int[int] # [unsubscriptable-object] +var_bytestring2: typing.ByteString[int] # [unsubscriptable-object] +var_hashable2: typing.Hashable[int] # [unsubscriptable-object] +var_sized2: typing.Sized[int] # [unsubscriptable-object] diff --git a/tests/functional/g/generic_alias/generic_alias_typing.rc b/tests/functional/g/generic_alias/generic_alias_typing.rc new file mode 100644 index 0000000000..a17bb22daf --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_typing.rc @@ -0,0 +1,2 @@ +[testoptions] +min_pyver=3.7 diff --git a/tests/functional/g/generic_alias/generic_alias_typing.txt b/tests/functional/g/generic_alias/generic_alias_typing.txt new file mode 100644 index 0000000000..8942ca71a2 --- /dev/null +++ b/tests/functional/g/generic_alias/generic_alias_typing.txt @@ -0,0 +1,19 @@ +unsubscriptable-object:66:0::Value 'typing.ByteString' is unsubscriptable +unsubscriptable-object:67:0::Value 'typing.Hashable' is unsubscriptable +unsubscriptable-object:68:0::Value 'typing.Sized' is unsubscriptable +abstract-method:72:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:75:0:DerivedIterable:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:78:0:DerivedCollection:Method '__contains__' is abstract in class 'Container' but is not overridden +abstract-method:78:0:DerivedCollection:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:78:0:DerivedCollection:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:100:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden +abstract-method:100:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:105:0:CustomAbstractCls2:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:105:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:107:0:CustomImplementation:Method '__iter__' is abstract in class 'Iterable' but is not overridden +abstract-method:107:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden +abstract-method:118:0:DerivedIterable2:Method '__iter__' is abstract in class 'Iterable' but is not overridden +unsubscriptable-object:138:9::Value 'int' is unsubscriptable +unsubscriptable-object:139:17::Value 'typing.ByteString' is unsubscriptable +unsubscriptable-object:140:15::Value 'typing.Hashable' is unsubscriptable +unsubscriptable-object:141:12::Value 'typing.Sized' is unsubscriptable