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

TypeAlias not conforming to PascalCase to throw invalid-name #7081

Closed
jamesbraza opened this issue Jun 28, 2022 · 5 comments · Fixed by #7116
Closed

TypeAlias not conforming to PascalCase to throw invalid-name #7081

jamesbraza opened this issue Jun 28, 2022 · 5 comments · Fixed by #7116
Assignees
Labels
C: invalid-name Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@jamesbraza
Copy link

Current problem

Python typing.TypeAlias are an alias to a type (as we all know). Thus, I think TypeAlias should have to follow type naming conventions (e.g. PascalCase for classes, unless a primitive).

In other words, we should see TypeAlias really as not much different from a novel class in regards to naming conventions. I think pylint is the tool to help enforce this!

Desired solution

Please see the below code snippet

# pylint: disable=missing-module-docstring

from threading import Thread  # Placeholder class
from typing import TypeAlias

ThreadAlias: TypeAlias = Thread  # Say nothing here
THREAD_ALIAS: TypeAlias = Thread  # Throw invalid-name
# C0103: Type alias "THREAD_ALIAS" doesn't conform to PascalCase naming style (invalid-name)

To restate, I think pylint should error on CLASS_NAME_ALIAS with invalid-name.

Additional context

See the TypeAlias PEP: https://peps.python.org/pep-0613/#error-messaging

They use ClassName, not CLASS_NAME.

@jamesbraza jamesbraza added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jun 28, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Proposal 📨 Needs decision 🔒 Needs a decision before implemention or rejection C: invalid-name and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jun 28, 2022
@cdce8p
Copy link
Member

cdce8p commented Jun 29, 2022

I agree. TypeAlias names should be PascalCase. We could even argue that its good practice to suffix them with Type (although I wouldn't enforce that). The difficulty will be detecting that a variable is actually a type alias. That's only really possible with the TypeAlias annotation. Maybe a few other common cases could be handled too, without the explicit TypeAlias: Union or Callable.

@DanielNoord Would you like to take a look at this one? I suspect it will be quite similar to the name check for TypeVars.

@Pierre-Sassoulas Pierre-Sassoulas added Enhancement ✨ Improvement to a component Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Proposal 📨 Needs decision 🔒 Needs a decision before implemention or rejection labels Jun 29, 2022
@DanielNoord
Copy link
Collaborator

Based on previous experiences I think narrowing down the tests are very important here. An initial draft:

"""Test cases for invalid-name for TypeAlias with default settings"""
# pylint: disable=too-few-public-methods,line-too-long

from typing import Callable, TypeAlias, Union

# PascalCase names
GoodName: TypeAlias = int
_GoodName: TypeAlias = int
__GoodName: TypeAlias = int
AnotherGoodName = Union[int, str]
AlsoGoodName = Callable

# Non-PascalCase names
BADName: TypeAlias = int # [invalid-name]
BadNAME: TypeAlias = int  # [invalid-name]
badName: TypeAlias = int  # [invalid-name]
BADNAMEType: TypeAlias = int # [invalid-name]
ANOTHERBADNAME = Union[int,str] # [invalid-name]
AlsoBADNAME = Callable  # [invalid-name]

Please tell me if you have any test cases that are missing.

@DanielNoord DanielNoord self-assigned this Jun 30, 2022
@cdce8p
Copy link
Member

cdce8p commented Jun 30, 2022

Based on previous experiences I think narrowing down the tests are very important here. An initial draft:
[...]
Please tell me if you have any test cases that are missing.

👍🏻 That's about what I had in mind. Maybe we can make also make sure that the name isn't suffixed with just T as to not mistake it with a TypeVar.

BadNameT: TypeAlias = int  # [invalid-name]

I'll also test the PR against Home Assistant once it's ready, just to make sure we don't miss anything else.

@jamesbraza
Copy link
Author

jamesbraza commented Jun 30, 2022

Thank you all for adding color! @DanielNoord I agree with all your GoodNames .

I would like to include two more bad names:

  • BAD_NAME/_BAD_NAME/__BAD_NAME: (implying it's some constant, not a type alias)
  • TBadName: can also be confused with TypeVar

@DanielNoord
Copy link
Collaborator

I have decided to leave Callable out because of the following example:

do_something: Callable[[], int]

def do_something() -> int:
    return 1

Submitting the PR in a couple of minutes. Let me know if you want me to re-add it.

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 Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants