Skip to content

Add tests for NewType false positives #5542

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions tests/functional/a/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,13 @@ def func(one, two, three):


CALL = lambda *args: func(*args)


# Check that typing.NewType calls expect exactly one argument
import typing


ChildType = typing.NewType("AliasType", int)
ChildType() # [no-value-for-parameter]
ChildType(1)
ChildType(1, 2) # [too-many-function-args]
2 changes: 2 additions & 0 deletions tests/functional/a/arguments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ unexpected-keyword-arg:202:23:202:56:namedtuple_replace_issue_1036:Unexpected ke
no-value-for-parameter:215:0:215:24::No value for argument 'third' in function call:UNDEFINED
no-value-for-parameter:216:0:216:30::No value for argument 'second' in function call:UNDEFINED
unexpected-keyword-arg:217:0:217:43::Unexpected keyword argument 'fourth' in function call:UNDEFINED
no-value-for-parameter:270:0:270:11::No value for argument 'val' in constructor call:UNDEFINED
too-many-function-args:272:0:272:15::Too many positional arguments for constructor call:UNDEFINED
7 changes: 7 additions & 0 deletions tests/functional/i/iterable_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""
# pylint: disable=missing-docstring,invalid-name,too-few-public-methods,no-self-use,import-error,unused-argument,bad-mcs-method-argument,wrong-import-position,no-else-return, useless-object-inheritance, unnecessary-comprehension,redundant-u-string-prefix
from __future__ import print_function
import typing

# primitives
numbers = [1, 2, 3]
Expand Down Expand Up @@ -192,5 +193,11 @@ def __getattr__(self, attr):
pass


Subtype = typing.NewType("Subtype", typing.List[int])

for i in Subtype([1, 2, 3]):
pass


# Regression test for https://github.com/PyCQA/pylint/issues/6372
string_twos = "".join(str(*y) for _, *y in [[1, 2], [1, 2]])
20 changes: 10 additions & 10 deletions tests/functional/i/iterable_context.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
not-an-iterable:58:9:58:22::Non-iterable value powers_of_two is used in an iterating context:UNDEFINED
not-an-iterable:93:6:93:9::Non-iterable value A() is used in an iterating context:UNDEFINED
not-an-iterable:95:6:95:7::Non-iterable value B is used in an iterating context:UNDEFINED
not-an-iterable:96:9:96:12::Non-iterable value A() is used in an iterating context:UNDEFINED
not-an-iterable:100:9:100:10::Non-iterable value B is used in an iterating context:UNDEFINED
not-an-iterable:103:9:103:14::Non-iterable value range is used in an iterating context:UNDEFINED
not-an-iterable:107:9:107:13::Non-iterable value True is used in an iterating context:UNDEFINED
not-an-iterable:110:9:110:13::Non-iterable value None is used in an iterating context:UNDEFINED
not-an-iterable:113:9:113:12::Non-iterable value 8.5 is used in an iterating context:UNDEFINED
not-an-iterable:116:9:116:11::Non-iterable value 10 is used in an iterating context:UNDEFINED
not-an-iterable:59:9:59:22::Non-iterable value powers_of_two is used in an iterating context:UNDEFINED
not-an-iterable:94:6:94:9::Non-iterable value A() is used in an iterating context:UNDEFINED
not-an-iterable:96:6:96:7::Non-iterable value B is used in an iterating context:UNDEFINED
not-an-iterable:97:9:97:12::Non-iterable value A() is used in an iterating context:UNDEFINED
not-an-iterable:101:9:101:10::Non-iterable value B is used in an iterating context:UNDEFINED
not-an-iterable:104:9:104:14::Non-iterable value range is used in an iterating context:UNDEFINED
not-an-iterable:108:9:108:13::Non-iterable value True is used in an iterating context:UNDEFINED
not-an-iterable:111:9:111:13::Non-iterable value None is used in an iterating context:UNDEFINED
not-an-iterable:114:9:114:12::Non-iterable value 8.5 is used in an iterating context:UNDEFINED
not-an-iterable:117:9:117:11::Non-iterable value 10 is used in an iterating context:UNDEFINED
41 changes: 41 additions & 0 deletions tests/functional/m/member/member_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,44 @@ class Animal(Enum):
print(keyy)
for vall in Animal.__members__.values():
print(vall)


# Test for false positives on NewType classes
from typing import NewType

UserId = NewType("UserId", str)

some_id = UserId("id")
some_id.capitalize()
some_id.not_a_str_method() # [no-member]


class BaseClass:
def __init__(self, value):
self.value = value


ChildClass = NewType("ChildClass", BaseClass)


base = BaseClass(5)
base.value
base.bad_attr # [no-member]


child = ChildClass(base)
child.value
child.bad_attr # [no-member]


import pathlib

PathChild = NewType("PathChild", pathlib.PurePath)

path = pathlib.PurePath("/")
path.root
path.bad_attr # [no-member]

path_child = PathChild(path)
path_child.root
path_child.bad_attr # [no-member]
5 changes: 5 additions & 0 deletions tests/functional/m/member/member_checks.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ no-member:135:0:135:18::Class 'str' has no 'portocala' member:INFERENCE
no-member:170:19:170:32:NoDunderNameInInstance.__init__:Instance of 'NoDunderNameInInstance' has no '__name__' member:INFERENCE
no-member:176:14:176:23:InvalidAccessBySlots.__init__:Instance of 'InvalidAccessBySlots' has no 'teta' member:INFERENCE
no-member:208:13:208:20::Class 'Cls' has no 'BAZ' member; maybe 'BAR'?:INFERENCE
no-member:243:0:243:24::Instance of 'UserId' has no 'not_a_str_method' member:INFERENCE
no-member:256:0:256:13::Instance of 'BaseClass' has no 'bad_attr' member:INFERENCE
no-member:261:0:261:14::Instance of 'ChildClass' has no 'bad_attr' member:INFERENCE
no-member:270:0:270:13::Instance of 'PurePath' has no 'bad_attr' member:INFERENCE
no-member:274:0:274:19::Instance of 'PathChild' has no 'bad_attr' member:INFERENCE