Skip to content

Add new check "implicit-flag-alias" #8345

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

Merged
merged 59 commits into from
Feb 26, 2023
Merged

Add new check "implicit-flag-alias" #8345

merged 59 commits into from
Feb 26, 2023

Conversation

jayaddison
Copy link
Contributor

Type of Changes

Type
✨ New feature

Description

This changeset implements an implicit-flag-alias check that inspects classes derived from enum.Flag and looks for member values that share common bit positions.

The commented sample code below may help explain:

from enum import IntFlag

class Test(IntFlag):

    # valid cases:

    A = 1
    B = 2
    C = 2          # C is an alias (alternative name) for B, and is not highlighted by the check
    D = A | B      # D shares bit positions with A and B, but uses bitwise-operator syntax and is not highlighted

    # implicit-flag-alias cases:

    E = 3          # The value of E shares bit positions with A and B, so the check will highlight it

Closes #8102.
Replaces and continues from #8105 (origin branch accidentally deleted).

…riables, since we have restricted the domain to Integer types

Relates to commit b1e87c2.
… back to the original location it was added

Relates to commit 6969070.
In particular, this handles usage of the 'enum.auto' utility class, used to allocate suitable enum values automatically
… including py311-modelled representation of potentially-problematic flag members
Ok, my mistake: not one, but four more commits lined up...
…ehension to collect paired overlapping elements
… a common flag value for the integer representation of that standalone bit
… and shorten related variable names [skip ci]
…erlap_assignment' (it represents an assignment statement in the code-being-scanned) [skip ci]
… well-defined sort order)"

This reverts commit a57e37c.
…s in each flag's binary representation that are set to '1'
@codecov
Copy link

codecov bot commented Feb 26, 2023

Codecov Report

Merging #8345 (1fc1eac) into main (bd3bcb1) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main    #8345    +/-   ##
========================================
  Coverage   95.46%   95.47%            
========================================
  Files         176      177     +1     
  Lines       18543    18726   +183     
========================================
+ Hits        17702    17878   +176     
- Misses        841      848     +7     
Impacted Files Coverage Δ
pylint/checkers/classes/class_checker.py 93.71% <100.00%> (+0.14%) ⬆️
pylint/testutils/_primer/package_to_lint.py 76.81% <0.00%> (-23.19%) ⬇️
pylint/testutils/reporter_for_tests.py 93.02% <0.00%> (-0.32%) ⬇️
pylint/checkers/variables.py 97.30% <0.00%> (-0.08%) ⬇️
pylint/checkers/base/basic_error_checker.py 95.51% <0.00%> (-0.04%) ⬇️
pylint/checkers/base/basic_checker.py 97.87% <0.00%> (-0.03%) ⬇️
pylint/lint/run.py 86.36% <0.00%> (ø)
pylint/constants.py 100.00% <0.00%> (ø)
pylint/reporters/text.py 87.05% <0.00%> (ø)
pylint/checkers/stdlib.py 96.70% <0.00%> (ø)
... and 32 more

The documentation testing selectively enables lint checks based on the filename, so we need to rename the files to match the rule they are documenting
@Pierre-Sassoulas Pierre-Sassoulas added the Enhancement ✨ Improvement to a component label Feb 26, 2023
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.0 milestone Feb 26, 2023
… comparative good/bad cases

Co-authored-by: Pierre Sassoulas <[email protected]>
@jayaddison
Copy link
Contributor Author

Thanks again @Pierre-Sassoulas :)

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 1fc1eac

@Pierre-Sassoulas Pierre-Sassoulas merged commit 467b7b0 into pylint-dev:main Feb 26, 2023
@jayaddison jayaddison deleted the issue-8102/implicit-flag-overlap branch February 26, 2023 17:15
@jayaddison
Copy link
Contributor Author

Checking in to confirm that this looks like it's working as expected in v2.17.0 - I'll watch for any unexpected issues and/or false positives.

Tested with v2.17.0:

# flag-overlap.py
from enum import IntFlag

class Chroma(IntFlag):
    BLEU = 0
    RED = 1
    BLEU = 2
    GRÜNE = 3
    黄色 = 4
$ pylint flag-overlap.py 
************* Module flag-overlap
flag-overlap.py:7:4: W0213: Flag member <Chroma.GRÜNE: 3> shares bit positions with <Chroma.RED: 1> (3 & 1 = 1), <Chroma.BLEU: 2> (3 & 2 = 2) (implicit-flag-alias)
flag-overlap.py:7:4: C2401: Attribute name "GRÜNE" contains a non-ASCII character, consider renaming it. (non-ascii-name)
flag-overlap.py:8:4: C2401: Attribute name "黄色" contains a non-ASCII character, consider renaming it. (non-ascii-name)

------------------------------------------------------------------
Your code has been rated at 5.71/10 (previous run: 7.14/10, -1.43)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement ✨ Improvement to a component
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: implicit-flag-overlap
3 participants