Skip to content

Conversation

@jparise
Copy link
Member

@jparise jparise commented May 1, 2025

This is consistent with our CapWords-based N801 class name check, which shares a similar PEP 8 definition.

Resolves #245

This is consistent with our CapWords-based N801 class name check, which
shares a similar PEP 8 definition.

Resolves #245
@jparise jparise requested a review from sigmavirus24 May 1, 2025 12:50
#: N808
__NotGood = TypeVar("__NotGood")
#: Okay
__Good = TypeVar("__Good")
Copy link
Member

Choose a reason for hiding this comment

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

I disagree. I believe _T_co is fine, or _T, but I don't believe __T (double underscore) is a good example here.

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
__Good = TypeVar("__Good")
_Good = TypeVar("_Good")
#: N808
__NotGood = TypeVar("__NotGood")

Copy link
Member Author

@jparise jparise May 1, 2025

Choose a reason for hiding this comment

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

I don't have strong feelings either way. I haven't had a case (personally) where I would use a leading underscore, but it appears others do this fairly often:

https://github.com/search?q=lang%3Apython+%22%3D+TypeVar%28%27_%22+OR+%22%3D+TypeVar%28%5C%22_%22&type=code

Leading double underscores exist but are uncommon, so I think that's a good argument against supporting them:

https://github.com/search?q=lang%3Apython+%22%3D+TypeVar%28%27__%22+OR+%22%3D+TypeVar%28%5C%22__%22&type=code

yield self.err(body, 'N808', name=name)

if not name[:1].isupper():
stripped_name = name.lstrip('_')
Copy link
Member

Choose a reason for hiding this comment

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

I think the simplicity here is the attractive nuisance. I would probably bail on if name.startswith("__") because again, what's being asked for in the original issue is a single leading underscore. Double leading underscores for this are something I think we should draw a firm line in the sand about. I also don't agree with _T_co but am fine supporting it if someone can justify why they want a typevar that starts like that (aside from they're telling people to from <mod> import * which is just a bad reason to justify this).

I think we can simplify a lot of this logic though, because in retrospect, we can do

if name.startswith('__'):
    yield self.err(body, 'N808', name=name) # Maybe return to short-circuit things?

stripped_name = name.lstrip('_')
parts = stripped_name.rsplit('_', 1)
if not parts[0].istitle():
    yield self.err(body, 'N808', name=name)

# etc.

I don't think the __ is ever something we should not alert on frankly. It causes lots of silly magical things in the background.

Copy link
Member Author

Choose a reason for hiding this comment

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

I also have some other overall improvements to this function that I'll save for another pull request.

@jparise jparise changed the title Allow TypeVar names to start with underscores Allow TypeVar names to start with an underscore May 1, 2025
@jparise jparise requested a review from sigmavirus24 May 1, 2025 15:28
yield self.err(body, 'N808', name=name)

if not name[:1].isupper():
stripped_name = name.removeprefix('_')
Copy link
Member

Choose a reason for hiding this comment

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

TIL: removeprefix

@sigmavirus24 sigmavirus24 merged commit 9c83095 into main May 1, 2025
6 checks passed
@sigmavirus24 sigmavirus24 deleted the typevar-prefix branch May 1, 2025 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

N808 TypeVar names starting with a single underscore.

3 participants