Skip to content

Commit

Permalink
Improve invalid-slots-object message output (#6192)
Browse files Browse the repository at this point in the history
* Make `invalid-slots-object` confidence INFERENCE

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
  • Loading branch information
jpy-git and Pierre-Sassoulas committed Apr 9, 2022
1 parent dd54023 commit a1e7afb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
Expand Up @@ -9,6 +9,9 @@ Release date: TBA
..
Put new features here and also in 'doc/whatsnew/2.14.rst'

* Update ``invalid-slots-object`` message to show bad object rather than its inferred value.

Closes #6101

* Added new checker ``typevar-name-mismatch``: TypeVar must be assigned to a variable with the same name as its name argument.

Expand Down
4 changes: 4 additions & 0 deletions doc/whatsnew/2.14.rst
Expand Up @@ -60,6 +60,10 @@ Extensions
Other Changes
=============

* Update ``invalid-slots-object`` message to show bad object rather than its inferred value.

Closes #6101

* Removed the broken ``generate-man`` option.

Closes #5283
Expand Down
10 changes: 8 additions & 2 deletions pylint/checkers/classes/class_checker.py
Expand Up @@ -1429,12 +1429,18 @@ def _check_slots_elt(self, elt, node):
inferred.value, str
):
self.add_message(
"invalid-slots-object", args=inferred.as_string(), node=elt
"invalid-slots-object",
args=elt.as_string(),
node=elt,
confidence=INFERENCE,
)
continue
if not inferred.value:
self.add_message(
"invalid-slots-object", args=inferred.as_string(), node=elt
"invalid-slots-object",
args=elt.as_string(),
node=elt,
confidence=INFERENCE,
)

# Check if we have a conflict with a class variable.
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/s/slots_checks.py
Expand Up @@ -57,6 +57,12 @@ class SeventhBad(object): # [single-string-used-for-slots]
class EighthBad(object): # [single-string-used-for-slots]
__slots__ = deque.__name__

class NinthBad(object):
__slots__ = [str] # [invalid-slots-object]

class TenthBad(object):
__slots__ = [1 + 2 + 3] # [invalid-slots-object]

class PotentiallyGood(object):
__slots__ = func()

Expand Down
12 changes: 7 additions & 5 deletions tests/functional/s/slots_checks.txt
@@ -1,11 +1,13 @@
invalid-slots:36:0:36:9:Bad:Invalid __slots__ object:UNDEFINED
invalid-slots:39:0:39:15:SecondBad:Invalid __slots__ object:UNDEFINED
invalid-slots-object:43:22:43:23:ThirdBad:Invalid object '2' in __slots__, must contain only non empty strings:UNDEFINED
invalid-slots-object:43:22:43:23:ThirdBad:Invalid object '2' in __slots__, must contain only non empty strings:INFERENCE
invalid-slots:45:0:45:15:FourthBad:Invalid __slots__ object:UNDEFINED
invalid-slots-object:49:27:49:29:FifthBad:"Invalid object ""''"" in __slots__, must contain only non empty strings":UNDEFINED
invalid-slots-object:49:27:49:29:FifthBad:"Invalid object ""''"" in __slots__, must contain only non empty strings":INFERENCE
single-string-used-for-slots:51:0:51:14:SixthBad:Class __slots__ should be a non-string iterable:UNDEFINED
single-string-used-for-slots:54:0:54:16:SeventhBad:Class __slots__ should be a non-string iterable:UNDEFINED
single-string-used-for-slots:57:0:57:15:EighthBad:Class __slots__ should be a non-string iterable:UNDEFINED
class-variable-slots-conflict:85:17:85:24:ValueInSlotConflict:Value 'first' in slots conflicts with class variable:UNDEFINED
class-variable-slots-conflict:85:45:85:53:ValueInSlotConflict:Value 'fourth' in slots conflicts with class variable:UNDEFINED
class-variable-slots-conflict:85:36:85:43:ValueInSlotConflict:Value 'third' in slots conflicts with class variable:UNDEFINED
invalid-slots-object:61:17:61:20:NinthBad:Invalid object 'str' in __slots__, must contain only non empty strings:INFERENCE
invalid-slots-object:64:17:64:26:TenthBad:Invalid object '1 + 2 + 3' in __slots__, must contain only non empty strings:INFERENCE
class-variable-slots-conflict:91:17:91:24:ValueInSlotConflict:Value 'first' in slots conflicts with class variable:UNDEFINED
class-variable-slots-conflict:91:45:91:53:ValueInSlotConflict:Value 'fourth' in slots conflicts with class variable:UNDEFINED
class-variable-slots-conflict:91:36:91:43:ValueInSlotConflict:Value 'third' in slots conflicts with class variable:UNDEFINED

0 comments on commit a1e7afb

Please sign in to comment.