Skip to content

Commit f7e6850

Browse files
committed
Add generic alias test cases
1 parent 078ebea commit f7e6850

31 files changed

+1260
-0
lines changed

tests/functional/g/generic_alias/__init__.py

Whitespace-only changes.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"""Test generic alias support for stdlib types (added in PY39)."""
2+
# flake8: noqa
3+
# pylint: disable=missing-docstring,pointless-statement
4+
# pylint: disable=too-few-public-methods,multiple-statements,line-too-long
5+
import abc
6+
import collections
7+
import collections.abc
8+
import contextlib
9+
import re
10+
11+
# special
12+
tuple[int, int]
13+
type[int]
14+
collections.abc.Callable[[int], str]
15+
16+
# builtins
17+
dict[int, str]
18+
list[int]
19+
set[int]
20+
frozenset[int]
21+
22+
# collections
23+
collections.defaultdict[int, str]
24+
collections.OrderedDict[int, str]
25+
collections.ChainMap[int, str]
26+
collections.Counter[int]
27+
collections.deque[int]
28+
29+
# collections.abc
30+
collections.abc.Set[int]
31+
collections.abc.Collection[int]
32+
collections.abc.Container[int]
33+
collections.abc.ItemsView[int, str]
34+
collections.abc.KeysView[int]
35+
collections.abc.Mapping[int, str]
36+
collections.abc.MappingView[int]
37+
collections.abc.MutableMapping[int, str]
38+
collections.abc.MutableSequence[int]
39+
collections.abc.MutableSet[int]
40+
collections.abc.Sequence[int]
41+
collections.abc.ValuesView[int]
42+
43+
collections.abc.Iterable[int]
44+
collections.abc.Iterator[int]
45+
collections.abc.Generator[int, None, None]
46+
collections.abc.Reversible[int]
47+
48+
collections.abc.Coroutine[list[str], str, int]
49+
collections.abc.AsyncGenerator[int, None]
50+
collections.abc.AsyncIterable[int]
51+
collections.abc.AsyncIterator[int]
52+
collections.abc.Awaitable[int]
53+
54+
# contextlib
55+
contextlib.AbstractContextManager[int]
56+
contextlib.AbstractAsyncContextManager[int]
57+
58+
# re
59+
re.Pattern[str]
60+
re.Match[str]
61+
62+
63+
# unsubscriptable types
64+
collections.abc.Hashable
65+
collections.abc.Sized
66+
collections.abc.Hashable[int] # [unsubscriptable-object]
67+
collections.abc.Sized[int] # [unsubscriptable-object]
68+
69+
# subscriptable with Python 3.9
70+
collections.abc.ByteString[int]
71+
72+
73+
# Missing implementation for 'collections.abc' derived classes
74+
class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__
75+
pass
76+
77+
class DerivedIterable(collections.abc.Iterable[int]): # [abstract-method] # __iter__
78+
pass
79+
80+
class DerivedCollection(collections.abc.Collection[int]): # [abstract-method,abstract-method,abstract-method] # __contains__, __iter__, __len__
81+
pass
82+
83+
84+
# No implementation required for 'builtins' and 'collections' types
85+
class DerivedList(list[int]):
86+
pass
87+
88+
class DerivedSet(set[int]):
89+
pass
90+
91+
class DerivedOrderedDict(collections.OrderedDict[int, str]):
92+
pass
93+
94+
class DerivedListIterable(list[collections.abc.Iterable[int]]):
95+
pass
96+
97+
98+
# Multiple generic base classes
99+
class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method]
100+
pass
101+
102+
class CustomAbstractCls1(abc.ABC):
103+
pass
104+
class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,abstract-method] # __iter__, __len__
105+
pass
106+
class CustomImplementation(CustomAbstractCls2): # [abstract-method,abstract-method] # __iter__, __len__
107+
pass
108+
109+
110+
# Type annotations
111+
var_tuple: tuple[int, int]
112+
var_dict: dict[int, str]
113+
var_orderedDict: collections.OrderedDict[int, str]
114+
var_container: collections.abc.Container[int]
115+
var_sequence: collections.abc.Sequence[int]
116+
var_iterable: collections.abc.Iterable[int]
117+
var_awaitable: collections.abc.Awaitable[int]
118+
var_contextmanager: contextlib.AbstractContextManager[int]
119+
var_pattern: re.Pattern[int]
120+
var_bytestring: collections.abc.ByteString
121+
var_hashable: collections.abc.Hashable
122+
var_sized: collections.abc.Sized
123+
124+
# Type annotation with unsubscriptable type
125+
var_int: int[int] # [unsubscriptable-object]
126+
var_hashable2: collections.abc.Hashable[int] # [unsubscriptable-object]
127+
var_sized2: collections.abc.Sized[int] # [unsubscriptable-object]
128+
129+
# subscriptable with Python 3.9
130+
var_bytestring2: collections.abc.ByteString[int]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[testoptions]
2+
min_pyver=3.9
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
unsubscriptable-object:66:0::Value 'collections.abc.Hashable' is unsubscriptable
2+
unsubscriptable-object:67:0::Value 'collections.abc.Sized' is unsubscriptable
3+
abstract-method:74:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden
4+
abstract-method:77:0:DerivedIterable:Method '__iter__' is abstract in class 'Iterable' but is not overridden
5+
abstract-method:80:0:DerivedCollection:Method '__contains__' is abstract in class 'Container' but is not overridden
6+
abstract-method:80:0:DerivedCollection:Method '__iter__' is abstract in class 'Iterable' but is not overridden
7+
abstract-method:80:0:DerivedCollection:Method '__len__' is abstract in class 'Sized' but is not overridden
8+
abstract-method:99:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden
9+
abstract-method:99:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden
10+
abstract-method:104:0:CustomAbstractCls2:Method '__iter__' is abstract in class 'Iterable' but is not overridden
11+
abstract-method:104:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden
12+
abstract-method:106:0:CustomImplementation:Method '__iter__' is abstract in class 'Iterable' but is not overridden
13+
abstract-method:106:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden
14+
unsubscriptable-object:125:9::Value 'int' is unsubscriptable
15+
unsubscriptable-object:126:15::Value 'collections.abc.Hashable' is unsubscriptable
16+
unsubscriptable-object:127:12::Value 'collections.abc.Sized' is unsubscriptable
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""Test generic alias support for stdlib types (added in PY39).
2+
3+
Raise [unsubscriptable-object] error for PY37 and PY38.
4+
"""
5+
# flake8: noqa
6+
# pylint: disable=missing-docstring,pointless-statement
7+
# pylint: disable=too-few-public-methods,multiple-statements,line-too-long
8+
import abc
9+
import collections
10+
import collections.abc
11+
import contextlib
12+
import re
13+
14+
# special
15+
tuple[int, int] # [unsubscriptable-object]
16+
type[int] # [unsubscriptable-object]
17+
collections.abc.Callable[[int], str] # [unsubscriptable-object]
18+
19+
# builtins
20+
dict[int, str] # [unsubscriptable-object]
21+
list[int] # [unsubscriptable-object]
22+
set[int] # [unsubscriptable-object]
23+
frozenset[int] # [unsubscriptable-object]
24+
25+
# collections
26+
collections.defaultdict[int, str] # [unsubscriptable-object]
27+
collections.OrderedDict[int, str] # [unsubscriptable-object]
28+
collections.ChainMap[int, str] # [unsubscriptable-object]
29+
collections.Counter[int] # [unsubscriptable-object]
30+
collections.deque[int] # [unsubscriptable-object]
31+
32+
# collections.abc
33+
collections.abc.Set[int] # [unsubscriptable-object]
34+
collections.abc.Collection[int] # [unsubscriptable-object]
35+
collections.abc.Container[int] # [unsubscriptable-object]
36+
collections.abc.ItemsView[int, str] # [unsubscriptable-object]
37+
collections.abc.KeysView[int] # [unsubscriptable-object]
38+
collections.abc.Mapping[int, str] # [unsubscriptable-object]
39+
collections.abc.MappingView[int] # [unsubscriptable-object]
40+
collections.abc.MutableMapping[int, str] # [unsubscriptable-object]
41+
collections.abc.MutableSequence[int] # [unsubscriptable-object]
42+
collections.abc.MutableSet[int] # [unsubscriptable-object]
43+
collections.abc.Sequence[int] # [unsubscriptable-object]
44+
collections.abc.ValuesView[int] # [unsubscriptable-object]
45+
46+
collections.abc.Iterable[int] # [unsubscriptable-object]
47+
collections.abc.Iterator[int] # [unsubscriptable-object]
48+
collections.abc.Generator[int, None, None] # [unsubscriptable-object]
49+
collections.abc.Reversible[int] # [unsubscriptable-object]
50+
51+
collections.abc.Coroutine[list[str], str, int] # [unsubscriptable-object,unsubscriptable-object]
52+
collections.abc.AsyncGenerator[int, None] # [unsubscriptable-object]
53+
collections.abc.AsyncIterable[int] # [unsubscriptable-object]
54+
collections.abc.AsyncIterator[int] # [unsubscriptable-object]
55+
collections.abc.Awaitable[int] # [unsubscriptable-object]
56+
57+
# contextlib
58+
contextlib.AbstractContextManager[int] # [unsubscriptable-object]
59+
contextlib.AbstractAsyncContextManager[int] # [unsubscriptable-object]
60+
61+
# re
62+
re.Pattern[str] # [unsubscriptable-object]
63+
re.Match[str] # [unsubscriptable-object]
64+
65+
66+
# unsubscriptable types
67+
collections.abc.Hashable
68+
collections.abc.Sized
69+
collections.abc.Hashable[int] # [unsubscriptable-object]
70+
collections.abc.Sized[int] # [unsubscriptable-object]
71+
72+
# subscriptable with Python 3.9
73+
collections.abc.ByteString[int] # [unsubscriptable-object]
74+
75+
76+
# Missing implementation for 'collections.abc' derived classes
77+
class DerivedHashable(collections.abc.Hashable): # [abstract-method] # __hash__
78+
pass
79+
80+
class DerivedIterable(collections.abc.Iterable[int]): # [unsubscriptable-object]
81+
pass
82+
83+
class DerivedCollection(collections.abc.Collection[int]): # [unsubscriptable-object]
84+
pass
85+
86+
87+
# No implementation required for 'builtins' and 'collections' types
88+
class DerivedList(list[int]): # [unsubscriptable-object]
89+
pass
90+
91+
class DerivedSet(set[int]): # [unsubscriptable-object]
92+
pass
93+
94+
class DerivedOrderedDict(collections.OrderedDict[int, str]): # [unsubscriptable-object]
95+
pass
96+
97+
class DerivedListIterable(list[collections.abc.Iterable[int]]): # [unsubscriptable-object,unsubscriptable-object]
98+
pass
99+
100+
101+
# Multiple generic base classes
102+
class DerivedMultiple(collections.abc.Sized, collections.abc.Hashable): # [abstract-method,abstract-method]
103+
pass
104+
105+
class CustomAbstractCls1(abc.ABC):
106+
pass
107+
class CustomAbstractCls2(collections.abc.Sized, collections.abc.Iterable[CustomAbstractCls1]): # [abstract-method,unsubscriptable-object] # __len__
108+
pass
109+
class CustomImplementation(CustomAbstractCls2): # [abstract-method] # __len__
110+
pass
111+
112+
113+
# Type annotations
114+
var_tuple: tuple[int, int] # [unsubscriptable-object]
115+
var_dict: dict[int, str] # [unsubscriptable-object]
116+
var_orderedDict: collections.OrderedDict[int, str] # [unsubscriptable-object]
117+
var_container: collections.abc.Container[int] # [unsubscriptable-object]
118+
var_sequence: collections.abc.Sequence[int] # [unsubscriptable-object]
119+
var_iterable: collections.abc.Iterable[int] # [unsubscriptable-object]
120+
var_awaitable: collections.abc.Awaitable[int] # [unsubscriptable-object]
121+
var_contextmanager: contextlib.AbstractContextManager[int] # [unsubscriptable-object]
122+
var_pattern: re.Pattern[int] # [unsubscriptable-object]
123+
var_bytestring: collections.abc.ByteString
124+
var_hashable: collections.abc.Hashable
125+
var_sized: collections.abc.Sized
126+
127+
# Type annotation with unsubscriptable type
128+
var_int: int[int] # [unsubscriptable-object]
129+
var_hashable2: collections.abc.Hashable[int] # [unsubscriptable-object]
130+
var_sized2: collections.abc.Sized[int] # [unsubscriptable-object]
131+
132+
# subscriptable with Python 3.9
133+
var_bytestring2: collections.abc.ByteString[int] # [unsubscriptable-object]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[testoptions]
2+
min_pyver=3.7
3+
max_pyver=3.9
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
unsubscriptable-object:15:0::Value 'tuple' is unsubscriptable
2+
unsubscriptable-object:16:0::Value 'type' is unsubscriptable
3+
unsubscriptable-object:17:0::Value 'collections.abc.Callable' is unsubscriptable
4+
unsubscriptable-object:20:0::Value 'dict' is unsubscriptable
5+
unsubscriptable-object:21:0::Value 'list' is unsubscriptable
6+
unsubscriptable-object:22:0::Value 'set' is unsubscriptable
7+
unsubscriptable-object:23:0::Value 'frozenset' is unsubscriptable
8+
unsubscriptable-object:26:0::Value 'collections.defaultdict' is unsubscriptable
9+
unsubscriptable-object:27:0::Value 'collections.OrderedDict' is unsubscriptable
10+
unsubscriptable-object:28:0::Value 'collections.ChainMap' is unsubscriptable
11+
unsubscriptable-object:29:0::Value 'collections.Counter' is unsubscriptable
12+
unsubscriptable-object:30:0::Value 'collections.deque' is unsubscriptable
13+
unsubscriptable-object:33:0::Value 'collections.abc.Set' is unsubscriptable
14+
unsubscriptable-object:34:0::Value 'collections.abc.Collection' is unsubscriptable
15+
unsubscriptable-object:35:0::Value 'collections.abc.Container' is unsubscriptable
16+
unsubscriptable-object:36:0::Value 'collections.abc.ItemsView' is unsubscriptable
17+
unsubscriptable-object:37:0::Value 'collections.abc.KeysView' is unsubscriptable
18+
unsubscriptable-object:38:0::Value 'collections.abc.Mapping' is unsubscriptable
19+
unsubscriptable-object:39:0::Value 'collections.abc.MappingView' is unsubscriptable
20+
unsubscriptable-object:40:0::Value 'collections.abc.MutableMapping' is unsubscriptable
21+
unsubscriptable-object:41:0::Value 'collections.abc.MutableSequence' is unsubscriptable
22+
unsubscriptable-object:42:0::Value 'collections.abc.MutableSet' is unsubscriptable
23+
unsubscriptable-object:43:0::Value 'collections.abc.Sequence' is unsubscriptable
24+
unsubscriptable-object:44:0::Value 'collections.abc.ValuesView' is unsubscriptable
25+
unsubscriptable-object:46:0::Value 'collections.abc.Iterable' is unsubscriptable
26+
unsubscriptable-object:47:0::Value 'collections.abc.Iterator' is unsubscriptable
27+
unsubscriptable-object:48:0::Value 'collections.abc.Generator' is unsubscriptable
28+
unsubscriptable-object:49:0::Value 'collections.abc.Reversible' is unsubscriptable
29+
unsubscriptable-object:51:0::Value 'collections.abc.Coroutine' is unsubscriptable
30+
unsubscriptable-object:51:26::Value 'list' is unsubscriptable
31+
unsubscriptable-object:52:0::Value 'collections.abc.AsyncGenerator' is unsubscriptable
32+
unsubscriptable-object:53:0::Value 'collections.abc.AsyncIterable' is unsubscriptable
33+
unsubscriptable-object:54:0::Value 'collections.abc.AsyncIterator' is unsubscriptable
34+
unsubscriptable-object:55:0::Value 'collections.abc.Awaitable' is unsubscriptable
35+
unsubscriptable-object:58:0::Value 'contextlib.AbstractContextManager' is unsubscriptable
36+
unsubscriptable-object:59:0::Value 'contextlib.AbstractAsyncContextManager' is unsubscriptable
37+
unsubscriptable-object:62:0::Value 're.Pattern' is unsubscriptable
38+
unsubscriptable-object:63:0::Value 're.Match' is unsubscriptable
39+
unsubscriptable-object:69:0::Value 'collections.abc.Hashable' is unsubscriptable
40+
unsubscriptable-object:70:0::Value 'collections.abc.Sized' is unsubscriptable
41+
unsubscriptable-object:73:0::Value 'collections.abc.ByteString' is unsubscriptable
42+
abstract-method:77:0:DerivedHashable:Method '__hash__' is abstract in class 'Hashable' but is not overridden
43+
unsubscriptable-object:80:22:DerivedIterable:Value 'collections.abc.Iterable' is unsubscriptable
44+
unsubscriptable-object:83:24:DerivedCollection:Value 'collections.abc.Collection' is unsubscriptable
45+
unsubscriptable-object:88:18:DerivedList:Value 'list' is unsubscriptable
46+
unsubscriptable-object:91:17:DerivedSet:Value 'set' is unsubscriptable
47+
unsubscriptable-object:94:25:DerivedOrderedDict:Value 'collections.OrderedDict' is unsubscriptable
48+
unsubscriptable-object:97:31:DerivedListIterable:Value 'collections.abc.Iterable' is unsubscriptable
49+
unsubscriptable-object:97:26:DerivedListIterable:Value 'list' is unsubscriptable
50+
abstract-method:102:0:DerivedMultiple:Method '__hash__' is abstract in class 'Hashable' but is not overridden
51+
abstract-method:102:0:DerivedMultiple:Method '__len__' is abstract in class 'Sized' but is not overridden
52+
abstract-method:107:0:CustomAbstractCls2:Method '__len__' is abstract in class 'Sized' but is not overridden
53+
unsubscriptable-object:107:48:CustomAbstractCls2:Value 'collections.abc.Iterable' is unsubscriptable
54+
abstract-method:109:0:CustomImplementation:Method '__len__' is abstract in class 'Sized' but is not overridden
55+
unsubscriptable-object:114:11::Value 'tuple' is unsubscriptable
56+
unsubscriptable-object:115:10::Value 'dict' is unsubscriptable
57+
unsubscriptable-object:116:17::Value 'collections.OrderedDict' is unsubscriptable
58+
unsubscriptable-object:117:15::Value 'collections.abc.Container' is unsubscriptable
59+
unsubscriptable-object:118:14::Value 'collections.abc.Sequence' is unsubscriptable
60+
unsubscriptable-object:119:14::Value 'collections.abc.Iterable' is unsubscriptable
61+
unsubscriptable-object:120:15::Value 'collections.abc.Awaitable' is unsubscriptable
62+
unsubscriptable-object:121:20::Value 'contextlib.AbstractContextManager' is unsubscriptable
63+
unsubscriptable-object:122:13::Value 're.Pattern' is unsubscriptable
64+
unsubscriptable-object:128:9::Value 'int' is unsubscriptable
65+
unsubscriptable-object:129:15::Value 'collections.abc.Hashable' is unsubscriptable
66+
unsubscriptable-object:130:12::Value 'collections.abc.Sized' is unsubscriptable
67+
unsubscriptable-object:133:17::Value 'collections.abc.ByteString' is unsubscriptable

0 commit comments

Comments
 (0)