Commits

Sylvain Thénault committed 50a1da7

[design analysis] fix badly implemented protocol for read-only containers like tuple. Close #25

  • Participants
  • Parent commits 443e97c

Comments (0)

Files changed (4)

 
     * bitbucket #37: check for unbalanced unpacking in assignments
 
+    * bitbucket #25: fix incomplete-protocol false positive for read-only
+      containers like tuple
+
     * bitbucket #16: fix False positive E1003 on Python 3 for argument-less super()
  
     * bitbucket #6: put back documentation in source distribution

File checkers/design_analysis.py

 SPECIAL_METHODS = [('Context manager', set(('__enter__',
                                             '__exit__',))),
                    ('Container', set(('__len__',
-                                      '__getitem__',
-                                      '__setitem__',
-                                      '__delitem__',))),
-                   ('Callable', set(('__call__',))),
+                                      '__getitem__',))),
+                   ('Mutable container', set(('__setitem__',
+                                              '__delitem__',))),
                    ]
 
 class SpecialMethodChecker(object):
         required_methods_found = methods_required & self.methods_found
         if required_methods_found == methods_required:
             return True
-        if required_methods_found != set():
-            required_methods_missing  = methods_required - self.methods_found
+        if required_methods_found:
+            required_methods_missing = methods_required - self.methods_found
             self.on_error((protocol,
                            ', '.join(sorted(required_methods_found)),
                            ', '.join(sorted(required_methods_missing))))

File test/input/func_special_methods.py

     def __iter__(self):
         pass
 
-class BadContainer(object):
+class BadROContainer(object):
     def __init__(self):
         pass
     def __len__(self):
     def __iter__(self):
         pass
 
-class Callable(object):
-    def __call__(self):
-        pass
+
+class BadContainer(object):
     def __init__(self):
         pass
+    def __len__(self):
+        return 0
+    def __getitem__(self, key, value):
+        pass
+    def __setitem__(self, key, value):
+        pass
 
+class MyTuple(tuple):
+    def __getitem__(self, index):
+        return super(MyTuple, self).__getitem__(index)

File test/messages/func_special_methods.txt

 R: 12:BadContextManager: Badly implemented Context manager, implements __enter__ but not __exit__
 R: 12:BadContextManager: Too few public methods (0/2)
-R: 32:BadContainer: Badly implemented Container, implements __len__, __setitem__ but not __delitem__, __getitem__
-R: 32:BadContainer: Too few public methods (0/2)
-
+R: 32:BadROContainer: Badly implemented Container, implements __len__ but not __getitem__
+R: 32:BadROContainer: Badly implemented Mutable container, implements __setitem__ but not __delitem__
+R: 32:BadROContainer: Too few public methods (0/2)
+R: 43:BadContainer: Badly implemented Mutable container, implements __setitem__ but not __delitem__