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

Add generic alias test cases #4239

Merged
merged 1 commit into from
Apr 10, 2021
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
Empty file.
130 changes: 130 additions & 0 deletions tests/functional/g/generic_alias/generic_alias_collections.py
Original file line number Diff line number Diff line change
@@ -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]
2 changes: 2 additions & 0 deletions tests/functional/g/generic_alias/generic_alias_collections.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[testoptions]
min_pyver=3.9
16 changes: 16 additions & 0 deletions tests/functional/g/generic_alias/generic_alias_collections.txt
Original file line number Diff line number Diff line change
@@ -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
133 changes: 133 additions & 0 deletions tests/functional/g/generic_alias/generic_alias_collections_py37.py
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[testoptions]
min_pyver=3.7
max_pyver=3.9
Original file line number Diff line number Diff line change
@@ -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
Loading