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 support for type variable name convention check #235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

itaihay
Copy link

@itaihay itaihay commented Jan 27, 2025

Proposed implementation for #163.

Adds support for type variable name convention check, as mentioned in PEP8:

Names of type variables introduced in PEP 484 should normally use CapWords preferring short names: T, AnyStr, Num. It is recommended to add suffixes _co or _contra to the variables used to declare covariant or contravariant behavior correspondingly:

Proposed name is N808 due to the relationship with the class name convention.

It's my first time contributing, so please let me know if anything needs to be done differently!
Thank you

Copy link
Member

@jparise jparise left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

Do you think we should consider the TypeVar's arguments? Otherwise, we can only check if the given name matches one of the allowed conventions, but we can't check if the name matches the TypeVar's specific values.

For example, would we want these mismatched forms to raise warnings?

  • Var = TypeVar("Var", covariant=True)
  • Var_co = TypeVar("Var", contravariant=True)
  • Var_contra = TypeVar("Var", covariant=True)

except AttributeError:
continue

if _ignored(name, ignore) or func_name != "TypeVar":
Copy link
Member

Choose a reason for hiding this comment

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

As a small optimization, let's check the cheaper expression first:

Suggested change
if _ignored(name, ignore) or func_name != "TypeVar":
if func_name != "TypeVar" or _ignored(name, ignore):

Comment on lines +295 to +297
underscore_split = name.split('_')
suffix = underscore_split[-1]
if len(underscore_split) > 2 or (suffix != 'co' and suffix != 'contra'):
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 this might be a little clearer if it focuses on a single suffix value:

Suggested change
underscore_split = name.split('_')
suffix = underscore_split[-1]
if len(underscore_split) > 2 or (suffix != 'co' and suffix != 'contra'):
parts = name.split('_')
suffix = parts[-1] if len(parts) > 2 else ''
if suffix and suffix != 'co' and suffix != 'contra':

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.

2 participants