Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

invalid_length_returned.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Check invalid value returned by __len__ """
  2. # pylint: disable=too-few-public-methods,missing-docstring,no-self-use,import-error
  3. import sys
  4. import six
  5. from missing import Missing
  6. class FirstGoodLen(object):
  7. """__len__ returns <type 'int'>"""
  8. def __len__(self):
  9. return 0
  10. class SecondGoodLen(object):
  11. """__len__ returns <type 'long'>"""
  12. def __len__(self):
  13. return sys.maxsize + 1
  14. class LenMetaclass(type):
  15. def __len__(cls):
  16. return 1
  17. @six.add_metaclass(LenMetaclass)
  18. class ThirdGoodLen(object):
  19. """Length through the metaclass."""
  20. class FirstBadLen(object):
  21. """ __len__ returns a negative integer """
  22. def __len__(self): # [invalid-length-returned]
  23. return -1
  24. class SecondBadLen(object):
  25. """ __len__ returns non-int """
  26. def __len__(self): # [invalid-length-returned]
  27. return 3.0
  28. class ThirdBadLen(object):
  29. """ __len__ returns node which does not have 'value' in AST """
  30. def __len__(self): # [invalid-length-returned]
  31. return lambda: 3
  32. class AmbigousLen(object):
  33. """ Uninferable return value """
  34. __len__ = lambda self: Missing
  35. class AnotherAmbiguousLen(object):
  36. """Potential uninferable return value"""
  37. def __len__(self):
  38. return int(Missing)