-
-
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
Create a TypeVar
style for invalid-name
#5894
Conversation
fc94327
to
4abbdf8
Compare
Pull Request Test Coverage Report for Build 1986086805
💛 - Coveralls |
4abbdf8
to
fb0fe5d
Compare
pylintrc
Outdated
# Naming style used for type variables. Default is PascalCase. | ||
typevar-naming-style=PascalCase |
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 believe this was the most recent discussion we had: Should we add a typevar-naming-style
add all or only have a typevar-rgx
option?
The arguments so far
- If we say the default is
PascalCase
, it will be difficult to explain whyPathType
is invalid andPathT
valid when both are PascalCase. - It's unclear what the regex patterns for the other styles should be and how they should interact with the
_co
and_contra
suffixes. - Having only the the rgx option (with default) would still allow for customizability if wanted.
- What should be done in similar cases, e.g. for ParamSpec names or Type aliases?
Changing it should be possible if we modify _create_naming_rules
and _create_naming_options
to allow for rgx only options.
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 was hesitant to do this at first, but turned out to be relatively easy to implement. I have pushed an iteration which does this to this branch. This basically supersedes #5221. I'm also fine with pushing it to that branch to keep the discussion in one place, but perhaps a relatively clean slate is helpful here.
fb0fe5d
to
b0fe891
Compare
TypeVar
name checking boilerplateTypeVar
style for invalid-name
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.
This looks like the solution I had in mind. 👍🏻
I think it's a good idea to do the changes here and not in the old PR.
pylintrc
Outdated
# 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 comment
The 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.
https://github.com/PyCQA/pylint/blob/d01e5e167834097ff7e5bf26a069aa1339669183/pylint/config/option.py#L102
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 checked. If it is empty the setting will be None
(I think this is due to how the config file is parsed). The following lines make sure that the pattern is only picked up if the pattern is set. (Note that this is also how this works for all other patterns).
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 comment
The reason will be displayed to describe this comment to others. Learn more.
An empty setting will be re.compile('')
and thus overwrite regexps[name_type]
. That isn't bad, we just can't leave it empty in pylintrc
. Otherwise TypeVar names in pylint itself won't be checked. That's similar to the other regex settings in pylintrc, eg. class-rgx
.
Alternatively, an possibly my preference would be the comment out that line. Similar to #class-const-rgx=
.
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.
Ah I understand why I was mistaken. The functional test don't actually use this pylintrc
so they passed even with this uncommented.
Commented it out!
Co-authored-by: Marc Mueller <[email protected]>
doc/user_guide/options.rst
Outdated
|
||
The following type of names are checked with a predefined pattern: | ||
|
||
- TypeVars. Accepted names include: `T`, `TypeVar`, `_CallableT`, `_T_co`. |
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
, and Bad names
.
I would prefer not to suggest TypeVar
. True it's valid, but it shouldn't be used IMO. Let's use something like MyClassT
to highlight PascalCase. For bad names, we should definitely mention something like T_Class
, and MyClassType
.
Some other names from the tests: AnyStr
, DeviceTypeT
, DeviceType
, CALLABLE_T
.
pylintrc
Outdated
# 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 comment
The reason will be displayed to describe this comment to others. Learn more.
An empty setting will be re.compile('')
and thus overwrite regexps[name_type]
. That isn't bad, we just can't leave it empty in pylintrc
. Otherwise TypeVar names in pylint itself won't be checked. That's similar to the other regex settings in pylintrc, eg. class-rgx
.
Alternatively, an possibly my preference would be the comment out that line. Similar to #class-const-rgx=
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove some of the GoodName
TypeVars here? We already have dedicated tests for the typevar-name-incorrect-variance
so I don't think we need to test this again here.
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)
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.
Looks good !
Co-authored-by: Marc Mueller <[email protected]>
Type of Changes
Description
Not sure if we want to do this, but:
After merging #5825, we can merge this. It only adds the necessary boilerplate for
typevar
name checking, but uses the pattern as set for classes.This makes the diff of #5221 even smaller and should help getting that merged asap after #5825 has been merged.
Closes #3401.