Skip to content
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

Improve TypeVar name regex #7322

Merged
merged 3 commits into from
Aug 19, 2022
Merged

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Aug 19, 2022

Description

Small improvements for the TypeVar name regex.

  • Simplify [^\W\da-z_] to [A-Z]
  • Add negative lookahead to prevent T or Type prefix. E.g. TAnyStr should be marked as invalid-name.

DanielNoord
DanielNoord previously approved these changes Aug 19, 2022
@coveralls
Copy link

coveralls commented Aug 19, 2022

Pull Request Test Coverage Report for Build 2888845479

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 95.252%

Totals Coverage Status
Change from base Build 2888370961: 0.0%
Covered Lines: 16872
Relevant Lines: 17713

💛 - Coveralls

@cdce8p
Copy link
Member Author

cdce8p commented Aug 19, 2022

@DanielNoord Do you know what I need to do for the checks to pass? Should I change the fragment suffix to .other or do I always need a Ref?

@DanielNoord
Copy link
Collaborator

@Pierre-Sassoulas We still can't seem to merge without updating branches 😢

@cdce8p cdce8p dismissed stale reviews from Pierre-Sassoulas and DanielNoord via a9ab795 August 19, 2022 10:39
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
@Pierre-Sassoulas
Copy link
Member

We still can't seem to merge without updating branches

I'm going to look into it, I'm not on mobile anymore. I hope you'll get admin-ship soon, be it the easy way or the hard way 😄

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like requiring a new review if the code change also auto-checked the "up to date code" option.

@DanielNoord DanielNoord enabled auto-merge (squash) August 19, 2022 10:45
@DanielNoord DanielNoord merged commit 1188340 into pylint-dev:main Aug 19, 2022
@cdce8p cdce8p deleted the improve-typevar-rgx branch August 19, 2022 10:49
@github-actions
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on astroid:
The following messages are now emitted:

  1. invalid-name:
    Type variable name "_NodesT2" doesn't conform to predefined naming style
    https://github.com/PyCQA/astroid/blob/main/astroid/nodes/node_ng.py#L54
  2. invalid-name:
    Type variable name "_NodesT3" doesn't conform to predefined naming style
    https://github.com/PyCQA/astroid/blob/main/astroid/nodes/node_ng.py#L55

Effect on pandas:
The following messages are now emitted:

  1. invalid-name:
    Type variable name "_TDT" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/main/pandas/core/indexes/datetimelike.py#L75

Effect on pytest:
The following messages are now emitted:

  1. invalid-name:
    Type variable name "TResult" doesn't conform to predefined naming style
    https://github.com/pytest-dev/pytest/blob/main/src/_pytest/runner.py#L264

Effect on sentry:
The following messages are now emitted:

  1. invalid-name:
    Type variable name "_K1" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/master/src/sentry/release_health/metrics.py#L90
  2. invalid-name:
    Type variable name "_K2" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/master/src/sentry/release_health/metrics.py#L91
  3. invalid-name:
    Type variable name "TDecoded" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/master/src/sentry/utils/codecs.py#L12
  4. invalid-name:
    Type variable name "TEncoded" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/master/src/sentry/utils/codecs.py#L13

This comment was generated for commit a9ab795

@cdce8p cdce8p mentioned this pull request Aug 27, 2022
@mattclay
Copy link

mattclay commented Sep 7, 2022

@cdce8p Where does the recommendation to avoid a T prefix come from?

@DanielNoord
Copy link
Collaborator

@mattclay I think this follows from what we have "seen in the wild". Note that you can allow them again by setting the typevar-rgx pattern config in your configuration file or on the command line.

@yilei
Copy link
Contributor

yilei commented Sep 20, 2022

The new regex also disallows things like _T1 _T2 which is a quite common practice

@mattclay
Copy link

@DanielNoord I don't know how common the T prefix is, but it's definitely used "in the wild". For example, it's used in the ansible project, where I counted at least 24 uses. Here's one example:

https://github.com/ansible/ansible/blob/f8e83264372d583a8f819f7bfe03a0f53f1abc0a/test/lib/ansible_test/_internal/config.py#L39

I realize the pattern can be customized, but it seems like an unnecessarily arbitrary stylistic restriction for a default.

@DanielNoord
Copy link
Collaborator

The new regex also disallows things like _T1 _T2 which is a quite common practice

This is problematic indeed.

I realize the pattern can be customized, but it seems like an unnecessarily arbitrary stylistic restriction for a default.

@cdce8p Do you have on opinion on this? If not I think we might want to revert this change. _T1 seems like it should be allowed and perhaps allowing other examples with a prefix of T is also better?

@cdce8p
Copy link
Member Author

cdce8p commented Oct 8, 2022

@cdce8p Where does the recommendation to avoid a T prefix come from?

I mainly looked at a number of typing PEPs as well as the TypeVar names used within typeshed as reference. Those do strongly prefer a T suffix over prefix which is why I advocated for that pattern.

The new regex also disallows things like _T1 _T2 which is a quite common practice

It's not as common as you may think. I would even argue that it's an antipattern which should be avoided except for very specific cases. It's much better to use different / more verbose names to avoid confusion and improve readability. Some common once include S, T, U, V, KT, VT. Just as an example take a look at the TypeVars defined in typeshed/stdlib/typing.pyi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: invalid-name Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants