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

invalid-name on TypeVar definition containing "Type" as part of the name #6003

Closed
leosmerling opened this issue Mar 27, 2022 · 5 comments
Closed

Comments

@leosmerling
Copy link

leosmerling commented Mar 27, 2022

Bug description

for the following Generic alias declaration:

a.py

"""module a"""
from typing import TypeVar

ConfigType = TypeVar("ConfigType")

pylint issues an invalid-name warning (apparently cause the Type suffix in the name).

C0103: Type variable name "ConfigType" doesn't conform to predefined naming style (invalid-name)

According to PEP-8 https://peps.python.org/pep-0008/#type-variable-names
TypeVar should use "CapWords" naming convention, with the option of _ + suffix,
but no mention about using "Type" or not as part of the name. (I might be missing something, so please correct me if I'm wrong)

Removing the "Type" suffix from the name fixes the issue.

"""module a"""
from typing import TypeVar

ConfigObj = TypeVar("ConfigObj")

I discovered this not based on PEP-8 but mostly cause other languages using generics types allow CapWords with any content to be used generic type names (like Java, Scala, C...). I might be wrong but I hope this helps clarifying the issue.

Configuration

No response

Command used

pylint a.py

Pylint output

************* Module a
a.py:4:0: C0103: Type variable name "ConfigType" doesn't conform to predefined naming style (invalid-name)

Expected behavior

No warning for correctly CapWord cased TypeVar.

Pylint version

pylint 2.13.2
astroid 2.11.2
Python 3.9.7 (default, Sep 16 2021, 13:09:58) 
[GCC 7.5.0]

OS / Environment

PopOS/Ubuntu:
Linux 5.16.11-76051611-generic #202202230823164624826121.10~2b22243
Wed Mar 2 20: x86_64 x86_64 x86_64 GNU/Linux

Additional dependencies

No response

@leosmerling leosmerling added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 27, 2022
@leosmerling leosmerling changed the title invalid-name on TypeVar definition invalid-name on TypeVar definition containing "Type" as part of the name Mar 27, 2022
@cdce8p cdce8p added Discussion 🤔 C: invalid-name and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 27, 2022
@cdce8p
Copy link
Member

cdce8p commented Mar 27, 2022

You're correct that PEP 8 only mentions CapWords. However, we deliberately chose to forbid the Type suffix. The reason was that it felt more like a TypeAlias that way. To make the distinction clear, it's better to suffix TypeVars with T.

I.e. ConfigTypeT. You can find some more examples here.

@leosmerling
Copy link
Author

leosmerling commented Mar 27, 2022

Thanks for the clarification. Then I assume I can adopt this convention in my code and this issue can be closed (at least from my point of view I don't have a strong opinion in favor or against it)

@cdce8p cdce8p closed this as completed Mar 27, 2022
lyz-code added a commit to lyz-code/blue-book that referenced this issue Mar 30, 2022
Using `UserT` is [not supported by pylint](pylint-dev/pylint#6003), use `UserT` instead.

feat(questionary#Exit when using control + c): Exit when using control + c

If you want the question to exit when it receives a `KeyboardInterrupt` event,
use `unsafe_ask` instead of `ask`.

fix(task_workflows): Update the task workflow of the month, and week plannings
@mwgamble
Copy link

It would be great if the messages users see when encountering errors like this were more helpful. A message like this:

Type variable name "StatusType" doesn't conform to predefined naming style

Is incredibly unhelpful. It doesn't tell me what the predefined naming style is, nor where to find out what it is. It doesn't even explicitly tell me what is wrong with the name that has been chosen, despite the fact that forbidding the Type suffix was apparently a conscious decision.

@Pierre-Sassoulas
Copy link
Member

I agree @mwgamble we have #6053 opened for this.

@Lx
Copy link

Lx commented Jun 7, 2022

You're correct that PEP 8 only mentions CapWords. However, we deliberately chose to forbid the Type suffix. The reason was that it felt more like a TypeAlias that way. To make the distinction clear, it's better to suffix TypeVars with T.

I.e. ConfigTypeT. You can find some more examples here.

Updated URL for referenced doc:
https://pylint.pycqa.org/en/latest/user_guide/messages/convention/invalid-name.html#predefined-naming-patterns

bouthilx added a commit to bouthilx/orion that referenced this issue Jul 4, 2022
Why:

*Type is an invalid name according to pylint. They recommend using the
suffix T instead of Type: pylint-dev/pylint#6003 (comment)
bouthilx added a commit to bouthilx/orion that referenced this issue Jul 4, 2022
Why:

*Type is an invalid name according to pylint. They recommend using the
suffix T instead of Type: pylint-dev/pylint#6003 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants