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

Add invalid-name check for TypeAlias names #7116

Merged
merged 4 commits into from
Feb 26, 2023

Conversation

DanielNoord
Copy link
Collaborator

  • Write a good description on what the PR does.
  • Add an entry to the change log describing the change in
    doc/whatsnew/2/2.15/index.rst (or doc/whatsnew/2/2.14/full.rst
    if the change needs backporting in 2.14). If necessary you can write
    details or offer examples on how the new change is supposed to work.
  • If you used multiple emails or multiple names when contributing, add your mails
    and preferred name in script/.contributors_aliases.json

Type of Changes

Type
✨ New feature

Description

Closes #7081.

@DanielNoord DanielNoord added Enhancement ✨ Improvement to a component C: invalid-name labels Jul 3, 2022
@DanielNoord DanielNoord requested a review from cdce8p July 3, 2022 14:33
@coveralls
Copy link

coveralls commented Jul 3, 2022

Pull Request Test Coverage Report for Build 3403162957

  • 30 of 32 (93.75%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.004%) to 95.376%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pylint/checkers/base/name_checker/checker.py 28 30 93.33%
Totals Coverage Status
Change from base Build 3401220436: -0.004%
Covered Lines: 17266
Relevant Lines: 18103

💛 - Coveralls

@github-actions

This comment has been minimized.

jamesbraza
jamesbraza previously approved these changes Jul 7, 2022
Copy link

@jamesbraza jamesbraza left a comment

Choose a reason for hiding this comment

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

I think you've done an excellent job @DanielNoord! Cheers!

@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Jul 21, 2022
@github-actions

This comment has been minimized.

@DanielNoord
Copy link
Collaborator Author

@cdce8p Just checking in. Do you want to review this before we merge this? Or can we merge without a review by you.

cdce8p
cdce8p previously requested changes Aug 16, 2022
Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

@cdce8p Just checking in. Do you want to review this before we merge this? Or can we merge without a review by you.

I had been away for a while working on other projects. If you want to reach me in these cases it's often best to DM me on Discord.

--
Regarding the PR, I left a few comments. Mostly left over references to Callable and an optimization suggestion. What we should think a little bit more about is the regex though. I checked the PR against Home Assistant and saw errors for IPAddress and IPNetwork. Those should be ok, IMO. Maybe just the T prefix should be disallowed, e.g. TBadName?

Edit: For TypeVars we do also allow IPAddressT so it would make sense to allow IPAddress for Type aliases.

doc/data/messages/i/invalid-name/details.rst Outdated Show resolved Hide resolved
doc/whatsnew/fragments/7801.new_check Outdated Show resolved Hide resolved
pylint/checkers/base/name_checker/checker.py Outdated Show resolved Hide resolved
pylint/checkers/base/name_checker/checker.py Outdated Show resolved Hide resolved
@@ -4,7 4,7 @@

Use 'typing.Callable' instead.
"""
# pylint: disable=missing-docstring,unsubscriptable-object
# pylint: disable=missing-docstring,unsubscriptable-object, invalid-name
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# pylint: disable=missing-docstring,unsubscriptable-object, invalid-name
# pylint: disable=missing-docstring,unsubscriptable-object,invalid-name

Might be just me, but I always find it irritating to see a space between error codes. Both for pylint and mypy. Similarly in the other test files.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh wow, I actually thought the preferred style was to separate them. I'm fine either way but thought that was the style we were going for.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I try to separate them myself 😄 (like arguments in a function call)

Copy link
Member

Choose a reason for hiding this comment

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

To be fair I've seen (and used) both 😄 For files I write myself, I do prefer without spaces. However if I'm just editing a random test file, I usually continue with the style that is present.

@DanielNoord
Copy link
Collaborator Author

@cdce8p Changed the regex. Let me know what you think.

This doesn't allow TBADname but I don't think there is a good way to reliably distinguish between TESTType and TTest. As in, when a T in the first place is a prefix or part of a longer word.

@github-actions

This comment has been minimized.

@DanielNoord
Copy link
Collaborator Author

After the primer comment I think it's better to not worry about the T. TokenInfo such as in black shouldn't really be flagged. Making changes.

@github-actions

This comment has been minimized.

@@ -40,7 40,8 @@
DEFAULT_PATTERNS = {
"typevar": re.compile(
r"^_{0,2}(?:[^\W\da-z_] |(?:[^\W\da-z_] [^\WA-Z_] ) T?(?<!Type))(?:_co(?:ntra)?)?$"
)
),
"typealias": re.compile(r"^_{0,2}[A-Z] [a-z] ([A-Z][a-z] )*$"),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"typealias": re.compile(r"^_{0,2}[A-Z] [a-z] ([A-Z][a-z] )*$"),
"typealias": re.compile(r"^_{0,2}(?!T[A-Z]|Type)[A-Z] [a-z] (?:[A-Z][a-z] )*$"),

Would this be a solution?
The negative lookahead would prevent cases like TBadName and TypeTodo but still allow TodoType. TodoType and TypeTodo could also be added as test cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, although it would still disallow TTCircuit as in the circuit of a tt race.

That's really far fetched but I don't really like how this is such an arbitrary rule that is really difficult to figure out if you stumble upon it. I don't think TTCircuit will be used very often but there might be real uses of TT at the start and I think it would be very frustrating to figure out why pylint disallows them.

What would you think about using the current pattern as the default value and using your suggestion as our own overwrite? We could potentially even add our own overwrite in the documentation of the message?

Copy link
Member

Choose a reason for hiding this comment

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

Getting the regex 100% correct is pretty difficult. What should we do for the pandas cases then (DatetimeNaTType, IntervalOrNA, ...)?

IMO we should try to find something that works for most cases and can prevent common issues. The remaining ones should be handled with pylint: disable[invalid-name]. In this case I believe it's a much more prevalent error to use a T or Type prefix then something like TTCircuit, so I think it would be worth it.

I just opened #7322 to fix the same issue for the TypeVar name style as I've seen _TRemote and _TCommand in HomeAssistant lately.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree that T and Type will be much more prevalent but I feel like flagging a TTCircuit would be considered a false positive. That's why I think we might want to be a little less strict by default and let users be more strict themselves. For me it falls within the false negatives vs. false positives preference, where I think the former is always more preferable.

Copy link
Member

Choose a reason for hiding this comment

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

For me it falls within the false negatives vs. false positives preference, where I think the former is always more preferable.

Usually I agree here. However, I would consider invalid-name to be at least somewhat special. It's one of the few checks which creates false-positives in order to highlight certain errors and I would think users might already be aware of that. Just an example, in HomeAssistant about 7-8% of all pylint: disable comments are for invalid-name (53 of 697).

Copy link
Member

Choose a reason for hiding this comment

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

(I was thinking of #2018 is the second most up-voted issues in the repo, but the regex is hiding the fact that the name is too short, #7305 aims to separate the two. Explaining everything in the regex is lower priority indeed)

Copy link
Member

Choose a reason for hiding this comment

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

Hey here could be an error, please check that. But if that's what you want, it's also fine.

Isn't that "convention" ? It's ok to not follow convention.

Copy link
Member

Choose a reason for hiding this comment

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

Hey here could be an error, please check that. But if that's what you want, it's also fine.

Isn't that "convention" ? It's ok to not follow convention.

True 😄
In that case, we might only need to better explain that it's just a convention and nothing more.

Copy link
Member

Choose a reason for hiding this comment

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

The issue might be that pep8 is wildly adopted (to a point that a lot of junior dev allow more time for PEP8 than to writing code that actually bring value). But PEP8 does not say anything about typevar/typealias yet so we're mixing a very popular convention with one that is less established.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have updated to use @cdce8p's suggestion and hope that the additional comments in details.rst will prevent any issues. I think Marc is right that we should try to enforce good behaviour here rather than prevent 2/3 opened issues about this.

@@ -4,7 4,7 @@

Use 'typing.Callable' instead.
"""
# pylint: disable=missing-docstring,unsubscriptable-object
# pylint: disable=missing-docstring,unsubscriptable-object, invalid-name
Copy link
Member

Choose a reason for hiding this comment

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

To be fair I've seen (and used) both 😄 For files I write myself, I do prefer without spaces. However if I'm just editing a random test file, I usually continue with the style that is present.

@github-actions

This comment has been minimized.

@Pierre-Sassoulas
Copy link
Member

Check fail should be fixed by #7321

@github-actions

This comment has been minimized.

@jamesbraza
Copy link

Can I help forward this at all? I hate to see all this work go stale for 4 months

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.16.0 milestone Nov 6, 2022
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.

LGTM, thank you for reminding us @jamesbraza. We have a lot going on and some MR that are hard/long to review get lost in the flow.

@github-actions

This comment has been minimized.

@cdce8p
Copy link
Member

cdce8p commented Nov 6, 2022

Can I help forward this at all? I hate to see all this work go stale for 4 months

I'll take another look at it soon.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.16.0, 2.17.0 Jan 8, 2023
@DanielNoord
Copy link
Collaborator Author

Can I help forward this at all? I hate to see all this work go stale for 4 months

I'll take another look at it soon.

@cdce8p Do you think you'll be able to have a look at this still?

@DanielNoord
Copy link
Collaborator Author

@Pierre-Sassoulas I would prefer to merge this even without the final review from Marc. If we get feedback on the regex we can always change it, but the underlying changes seem good to me. Are you okay with this?

@Pierre-Sassoulas
Copy link
Member

Yeah allright, let's merge.

@DanielNoord DanielNoord dismissed cdce8p’s stale review February 26, 2023 19:43

We are going ahead with merging this

@Pierre-Sassoulas Pierre-Sassoulas removed the Needs review 🔍 Needs to be reviewed by one or multiple more persons label Feb 26, 2023
@DanielNoord DanielNoord enabled auto-merge (squash) February 26, 2023 19:52
@codecov
Copy link

codecov bot commented Feb 26, 2023

Codecov Report

Merging #7116 (20ad037) into main (27a3984) will increase coverage by 0.00%.
The diff coverage is 96.49%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #7116    /-   ##
=======================================
  Coverage   95.47%   95.47%           
=======================================
  Files         177      178     1     
  Lines       18726    18776    50     
=======================================
  Hits        17878    17926    48     
- Misses        848      850     2     
Impacted Files Coverage Δ
pylint/checkers/base/name_checker/naming_style.py 100.00% <ø> (ø)
pylint/constants.py 100.00% <ø> (ø)
pylint/checkers/base/name_checker/checker.py 98.60% <93.54%> (-0.64%) ⬇️
pylint/checkers/bad_chained_comparison.py 100.00% <100.00%> (ø)
pylint/utils/linterstats.py 96.47% <100.00%> ( 0.04%) ⬆️

@DanielNoord
Copy link
Collaborator Author

Had to completely rebase as I was getting a lot of CI issues. Sorry for the loss of history, but this was the most sane way to do this.

@DanielNoord
Copy link
Collaborator Author

Also note that the recent PR broke main. I included a commit to fix this.

@DanielNoord DanielNoord merged commit e7ad3e6 into pylint-dev:main Feb 26, 2023
@DanielNoord DanielNoord deleted the typealias branch February 26, 2023 20:47
@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 alias name "SkipKlassT" doesn't conform to predefined naming style
    https://github.com/PyCQA/astroid/blob/fe5776279506e9fad59f0a4b850b6fa3e1a4220f/astroid/nodes/node_ng.py#L56

Effect on black:
The following messages are now emitted:

  1. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/brackets.py#L28
  2. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/nodes.py#L31
  3. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/debug.py#L10
  4. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/comments.py#L25
  5. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/linegen.py#L78
  6. invalid-name:
    Type alias name "LN" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/trans.py#L65
  7. invalid-name:
    Type alias name "TResult" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/trans.py#L71
  8. invalid-name:
    Type alias name "TMatchResult" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/black/trans.py#L72
  9. invalid-name:
    Type alias name "NL" doesn't conform to predefined naming style
    https://github.com/psf/black/blob/d9b8a6407e2f46304a8d36b18e4a73d8e0613519/src/blib2to3/pytree.py#L56

Effect on pandas:
The following messages are now emitted:

  1. invalid-name:
    Type alias name "ScalarLike_co" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/_typing.py#L72
  2. invalid-name:
    Type alias name "Manager2D" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/_typing.py#L302
  3. invalid-name:
    Type alias name "PositionalIndexer2D" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/_typing.py#L319
  4. invalid-name:
    Type alias name "DatetimeNaTType" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/_typing.py#L345
  5. invalid-name:
    Type alias name "DTScalarOrNaT" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/core/arrays/datetimelike.py#L157
  6. invalid-name:
    Type alias name "ArrowStringScalarOrNAT" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/core/arrays/string_arrow.py#L49
  7. invalid-name:
    Type alias name "IntervalSideT" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/core/arrays/interval.py#L111
  8. invalid-name:
    Type alias name "IntervalOrNA" doesn't conform to predefined naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/core/arrays/interval.py#L112

The following messages are no longer emitted:

  1. invalid-name:
    Class name "ScalarLike_co" doesn't conform to PascalCase naming style
    https://github.com/pandas-dev/pandas/blob/9203f9e90a5548275769b17f85b1fa06ec69e011/pandas/_typing.py#L72

Effect on sentry:
The following messages are now emitted:

  1. invalid-name:
    Type alias name "BaseApiResponseX" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/73dc9823fffc8a275071a5bb513f8610ea3f13b5/src/sentry/shared_integrations/client/base.py#L21
  2. invalid-name:
    Type alias name "SessionOrTransactionMRI" doesn't conform to predefined naming style
    https://github.com/getsentry/sentry/blob/73dc9823fffc8a275071a5bb513f8610ea3f13b5/src/sentry/testutils/cases.py#L169

Effect on coverage:
The following messages are now emitted:

  1. invalid-name:
    Type alias name "TTraceFileData" doesn't conform to predefined naming style
    https://github.com/nedbat/coveragepy/blob/499f4f52f738fac6133c9889b4a94b998628ac6f/coverage/types.py#L77
  2. invalid-name:
    Type alias name "TMorf" doesn't conform to predefined naming style
    https://github.com/nedbat/coveragepy/blob/499f4f52f738fac6133c9889b4a94b998628ac6f/coverage/types.py#L159
  3. invalid-name:
    Type alias name "TConfigParser" doesn't conform to predefined naming style
    https://github.com/nedbat/coveragepy/blob/499f4f52f738fac6133c9889b4a94b998628ac6f/coverage/config.py#L148

This comment was generated for commit 20ad037

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.

TypeAlias not conforming to PascalCase to throw invalid-name
5 participants