-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Create a TypeVar
style for invalid-name
#5894
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
Changes from 7 commits
b0fe891
0edcdef
58fd897
e9da6f0
93da592
ea087ab
49f9174
527e283
6bd9b6a
bb4617b
57ca521
cb78be4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -353,6 +353,10 @@ method-rgx=[a-z_][a-z0-9_]{2,}$ | |
# Naming hint for method names | ||
method-name-hint=[a-z_][a-z0-9_]{2,}$ | ||
|
||
# Regular expression which can overwrite the naming style set by typevar-naming-style. | ||
# If left empty, type variables will be checked with the set naming style. | ||
typevar-rgx= | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That doesn't work. If pattern is empty, it will match an empty pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked. If it is empty the setting will be custom_regex = getattr(self.config, custom_regex_setting_name, None)
if custom_regex is not None:
regexps[name_type] = custom_regex There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An empty setting will be Alternatively, an possibly my preference would be the comment out that line. Similar to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I understand why I was mistaken. The functional test don't actually use this Commented it out! |
||
|
||
# Regular expression which should only match function or class names that do | ||
# not require a docstring. Use ^(?!__init__$)_ to also check __init__. | ||
no-docstring-rgx=__.*__ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Test case for typevar-name-incorrect-variance with default settings""" | ||
# pylint: disable=too-few-public-methods | ||
|
||
from typing import TypeVar | ||
|
||
# PascalCase names with prefix | ||
GoodNameT = TypeVar("GoodNameT") | ||
_T = TypeVar("_T") | ||
_GoodNameT = TypeVar("_GoodNameT") | ||
__GoodNameT = TypeVar("__GoodNameT") | ||
GoodNameWithoutContra = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodNameWithoutContra", contravariant=True | ||
) | ||
GoodNameT_co = TypeVar("GoodNameT_co", covariant=True) | ||
GoodNameT_contra = TypeVar("GoodNameT_contra", contravariant=True) | ||
GoodBoundNameT = TypeVar("GoodBoundNameT", bound=int) | ||
GoodBoundNameT_co = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodBoundNameT_co", bound=int | ||
) | ||
GoodBoundNameT_contra = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodBoundNameT_contra", bound=int | ||
) | ||
GoodBoundNameT_co = TypeVar("GoodBoundNameT_co", bound=int, covariant=True) | ||
GoodBoundNameT_contra = TypeVar("GoodBoundNameT_contra", bound=int, contravariant=True) | ||
|
||
GoodBoundNameT_co = TypeVar("GoodBoundNameT_co", int, str, covariant=True) | ||
GoodBoundNameT_co = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodBoundNameT_co", int, str, contravariant=True | ||
) | ||
GoodBoundNameT_contra = TypeVar("GoodBoundNameT_contra", int, str, contravariant=True) | ||
GoodBoundNameT_contra = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodBoundNameT_contra", int, str, covariant=True | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we remove some of the Basically just keep # PascalCase names with prefix
GoodNameT = TypeVar("GoodNameT")
_T = TypeVar("_T")
_GoodNameT = TypeVar("_GoodNameT")
__GoodNameT = TypeVar("__GoodNameT")
GoodNameT_co = TypeVar("GoodNameT_co", covariant=True)
GoodNameT_contra = TypeVar("GoodNameT_contra", contravariant=True) |
||
|
||
# Some of these will create a RunTime error but serve as a regression test | ||
T = TypeVar( # [typevar-name-incorrect-variance] | ||
"T", covariant=True, contravariant=True | ||
) | ||
T = TypeVar("T", covariant=False, contravariant=False) | ||
T_co = TypeVar("T_co", covariant=True, contravariant=True) | ||
T_contra = TypeVar( # [typevar-name-incorrect-variance] | ||
"T_contra", covariant=True, contravariant=True | ||
) | ||
T_co = TypeVar("T_co", covariant=True, contravariant=False) | ||
T_contra = TypeVar("T_contra", covariant=False, contravariant=True) | ||
|
||
# PascalCase names without prefix | ||
AnyStr = TypeVar("AnyStr") | ||
DeviceTypeT = TypeVar("DeviceTypeT") | ||
CALLABLE_T = TypeVar("CALLABLE_T") # [invalid-name] | ||
DeviceType = TypeVar("DeviceType") # [invalid-name] | ||
GoodNameWithoutContra = TypeVar( # [typevar-name-incorrect-variance] | ||
"GoodNameWithoutContra", contravariant=True | ||
) | ||
DanielNoord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# camelCase names with prefix | ||
badName = TypeVar("badName") # [invalid-name] | ||
badNameWithoutContra = TypeVar( # [invalid-name, typevar-name-incorrect-variance]] | ||
"badNameWithoutContra", contravariant=True | ||
) | ||
DanielNoord marked this conversation as resolved.
Show resolved
Hide resolved
|
||
badName_co = TypeVar("badName_co", covariant=True) # [invalid-name] | ||
badName_contra = TypeVar("badName_contra", contravariant=True) # [invalid-name] | ||
|
||
# PascalCase names with lower letter prefix in tuple assignment | ||
( | ||
a_BadName, # [invalid-name] | ||
a_BadNameWithoutContra, # [invalid-name, typevar-name-incorrect-variance] | ||
) = TypeVar("a_BadName"), TypeVar("a_BadNameWithoutContra", contravariant=True) | ||
GoodName_co, a_BadName_contra = TypeVar( # [invalid-name] | ||
"GoodName_co", covariant=True | ||
), TypeVar("a_BadName_contra", contravariant=True) | ||
GoodName_co, VAR = TypeVar("GoodName_co", covariant=True), "a string" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
typevar-name-incorrect-variance:11:0:11:21::"Type variable ""GoodNameWithoutContra"" is contravariant, use ""GoodNameWithoutContra_contra"" instead":INFERENCE | ||
typevar-name-incorrect-variance:17:0:17:17::"Type variable ""GoodBoundNameT_co"" is invariant, use ""GoodBoundNameT"" instead":INFERENCE | ||
typevar-name-incorrect-variance:20:0:20:21::"Type variable ""GoodBoundNameT_contra"" is invariant, use ""GoodBoundNameT"" instead":INFERENCE | ||
typevar-name-incorrect-variance:27:0:27:17::"Type variable ""GoodBoundNameT_co"" is contravariant, use ""GoodBoundNameT_contra"" instead":INFERENCE | ||
typevar-name-incorrect-variance:31:0:31:21::"Type variable ""GoodBoundNameT_contra"" is covariant, use ""GoodBoundNameT_co"" instead":INFERENCE | ||
typevar-name-incorrect-variance:36:0:36:1::"Type variable ""T"" is covariant, use ""T_co"" instead":INFERENCE | ||
typevar-name-incorrect-variance:41:0:41:8::"Type variable ""T_contra"" is covariant, use ""T_co"" instead":INFERENCE | ||
invalid-name:50:0:50:10::"Type variable name ""CALLABLE_T"" doesn't conform to predefined naming style":HIGH | ||
invalid-name:51:0:51:10::"Type variable name ""DeviceType"" doesn't conform to predefined naming style":HIGH | ||
typevar-name-incorrect-variance:52:0:52:21::"Type variable ""GoodNameWithoutContra"" is contravariant, use ""GoodNameWithoutContra_contra"" instead":INFERENCE | ||
invalid-name:57:0:57:7::"Type variable name ""badName"" doesn't conform to predefined naming style":HIGH | ||
invalid-name:58:0:58:20::"Type variable name ""badNameWithoutContra"" doesn't conform to predefined naming style":HIGH | ||
typevar-name-incorrect-variance:58:0:58:20::"Type variable ""badNameWithoutContra"" is contravariant, use ""badNameWithoutContra_contra"" instead":INFERENCE | ||
invalid-name:61:0:61:10::"Type variable name ""badName_co"" doesn't conform to predefined naming style":HIGH | ||
invalid-name:62:0:62:14::"Type variable name ""badName_contra"" doesn't conform to predefined naming style":HIGH | ||
invalid-name:66:4:66:13::"Type variable name ""a_BadName"" doesn't conform to predefined naming style":HIGH | ||
invalid-name:67:4:67:26::"Type variable name ""a_BadNameWithoutContra"" doesn't conform to predefined naming style":HIGH | ||
typevar-name-incorrect-variance:67:4:67:26::"Type variable ""a_BadNameWithoutContra"" is contravariant, use ""a_BadNameWithoutContra_contra"" instead":INFERENCE | ||
invalid-name:69:13:69:29::"Type variable name ""a_BadName_contra"" doesn't conform to predefined naming style":HIGH |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if a table would make sense here? With
Name type
,Good names
, andBad names
.I would prefer not to suggest
TypeVar
. True it's valid, but it shouldn't be used IMO. Let's use something likeMyClassT
to highlight PascalCase. For bad names, we should definitely mention something likeT_Class
, andMyClassType
.Some other names from the tests:
AnyStr
,DeviceTypeT
,DeviceType
,CALLABLE_T
.