Skip to content

Commit 273177e

Browse files
committed
Add functional test for NewType member checking
#3162
1 parent 21cb688 commit 273177e

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

CONTRIBUTORS.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,3 +595,5 @@ contributors:
595595
* Eero Vuojolahti: contributor
596596

597597
* Kian-Meng, Ang: contributor
598+
599+
* Colin Atkinson (colatkinson): contributor

tests/functional/a/arguments.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,13 @@ def func(one, two, three):
260260

261261

262262
CALL = lambda *args: func(*args)
263+
264+
265+
# Check that typing.NewType calls expect exactly one argument
266+
import typing
267+
268+
269+
ChildType = typing.NewType("AliasType", int)
270+
ChildType() # [no-value-for-parameter]
271+
ChildType(1)
272+
ChildType(1, 2) # [too-many-function-args]

tests/functional/a/arguments.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ unexpected-keyword-arg:202:23:202:56:namedtuple_replace_issue_1036:Unexpected ke
3737
no-value-for-parameter:215:0:215:24::No value for argument 'third' in function call:UNDEFINED
3838
no-value-for-parameter:216:0:216:30::No value for argument 'second' in function call:UNDEFINED
3939
unexpected-keyword-arg:217:0:217:43::Unexpected keyword argument 'fourth' in function call:UNDEFINED
40+
no-value-for-parameter:270:0:270:11::No value for argument 'val' in constructor call:UNDEFINED
41+
too-many-function-args:272:0:272:15::Too many positional arguments for constructor call:UNDEFINED

tests/functional/m/member/member_checks.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,44 @@ class Animal(Enum):
231231
print(keyy)
232232
for vall in Animal.__members__.values():
233233
print(vall)
234+
235+
236+
# Test for false positives on NewType classes
237+
from typing import NewType
238+
239+
UserId = NewType("UserId", str)
240+
241+
some_id = UserId("id")
242+
some_id.capitalize()
243+
some_id.not_a_str_method() # [no-member]
244+
245+
246+
class BaseClass:
247+
def __init__(self, value):
248+
self.value = value
249+
250+
251+
ChildClass = NewType("ChildClass", BaseClass)
252+
253+
254+
base = BaseClass(5)
255+
base.value
256+
base.bad_attr # [no-member]
257+
258+
259+
child = ChildClass(base)
260+
child.value
261+
child.bad_attr # [no-member]
262+
263+
264+
import pathlib
265+
266+
PathChild = NewType("PathChild", pathlib.PurePath)
267+
268+
path = pathlib.PurePath("/")
269+
path.root
270+
path.bad_attr # [no-member]
271+
272+
path_child = PathChild(path)
273+
path_child.root
274+
path_child.bad_attr # [no-member]

tests/functional/m/member/member_checks.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ no-member:135:0:135:18::Class 'str' has no 'portocala' member:INFERENCE
1717
no-member:170:19:170:32:NoDunderNameInInstance.__init__:Instance of 'NoDunderNameInInstance' has no '__name__' member:INFERENCE
1818
no-member:176:14:176:23:InvalidAccessBySlots.__init__:Instance of 'InvalidAccessBySlots' has no 'teta' member:INFERENCE
1919
no-member:208:13:208:20::Class 'Cls' has no 'BAZ' member; maybe 'BAR'?:INFERENCE
20+
no-member:243:0:243:24::Instance of 'UserId' has no 'not_a_str_method' member:INFERENCE
21+
no-member:256:0:256:13::Instance of 'BaseClass' has no 'bad_attr' member:INFERENCE
22+
no-member:261:0:261:14::Instance of 'ChildClass' has no 'bad_attr' member:INFERENCE
23+
no-member:270:0:270:13::Instance of 'PurePath' has no 'bad_attr' member:INFERENCE
24+
no-member:274:0:274:19::Instance of 'PathChild' has no 'bad_attr' member:INFERENCE

0 commit comments

Comments
 (0)