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.

unidiomatic_typecheck.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Warnings for using type(x) == Y or type(x) is Y instead of isinstance(x, Y)."""
  2. # pylint: disable=missing-docstring,expression-not-assigned,redefined-builtin,invalid-name
  3. def simple_positives():
  4. type(42) is int # [unidiomatic-typecheck]
  5. type(42) is not int # [unidiomatic-typecheck]
  6. type(42) == int # [unidiomatic-typecheck]
  7. type(42) != int # [unidiomatic-typecheck]
  8. type(42) in [int] # [unidiomatic-typecheck]
  9. type(42) not in [int] # [unidiomatic-typecheck]
  10. def simple_inference_positives():
  11. alias = type
  12. alias(42) is int # [unidiomatic-typecheck]
  13. alias(42) is not int # [unidiomatic-typecheck]
  14. alias(42) == int # [unidiomatic-typecheck]
  15. alias(42) != int # [unidiomatic-typecheck]
  16. alias(42) in [int] # [unidiomatic-typecheck]
  17. alias(42) not in [int] # [unidiomatic-typecheck]
  18. def type_creation_negatives():
  19. type('Q', (object,), dict(a=1)) is int
  20. type('Q', (object,), dict(a=1)) is not int
  21. type('Q', (object,), dict(a=1)) == int
  22. type('Q', (object,), dict(a=1)) != int
  23. type('Q', (object,), dict(a=1)) in [int]
  24. type('Q', (object,), dict(a=1)) not in [int]
  25. def invalid_type_call_negatives(**kwargs):
  26. type(bad=7) is int
  27. type(bad=7) is not int
  28. type(bad=7) == int
  29. type(bad=7) != int
  30. type(bad=7) in [int]
  31. type(bad=7) not in [int]
  32. type('bad', 7) is int
  33. type('bad', 7) is not int
  34. type('bad', 7) == int
  35. type('bad', 7) != int
  36. type('bad', 7) in [int]
  37. type('bad', 7) not in [int]
  38. type(**kwargs) is int
  39. type(**kwargs) is not int
  40. type(**kwargs) == int
  41. type(**kwargs) != int
  42. type(**kwargs) in [int]
  43. type(**kwargs) not in [int]
  44. def local_var_shadowing_inference_negatives():
  45. type = lambda dummy: 7
  46. type(42) is int
  47. type(42) is not int
  48. type(42) == int
  49. type(42) != int
  50. type(42) in [int]
  51. type(42) not in [int]
  52. def parameter_shadowing_inference_negatives(type):
  53. type(42) is int
  54. type(42) is not int
  55. type(42) == int
  56. type(42) != int
  57. type(42) in [int]
  58. type(42) not in [int]
  59. def deliberate_subclass_check_negatives(b):
  60. type(42) is type(b)
  61. type(42) is not type(b)
  62. def type_of_literals_positives(a):
  63. type(a) is type([]) # [unidiomatic-typecheck]
  64. type(a) is not type([]) # [unidiomatic-typecheck]
  65. type(a) is type({}) # [unidiomatic-typecheck]
  66. type(a) is not type({}) # [unidiomatic-typecheck]
  67. type(a) is type("") # [unidiomatic-typecheck]
  68. type(a) is not type("") # [unidiomatic-typecheck]