1

What am I trying to do? Python's type hinting is useful for spotting certain coding mistakes, for instance if you accidentally try to call a type-hint decorated function with a mistake in the arguments. In statically typed languages we are also able to spot mistakes in operator mismatches, for example if you try to compare apples to oranges. Is it possible to extend the set of such warnings to include mismatched operators, for instance the equality operator ==, when coding python?

Specifically, I'm using PyCharm and would like to see a highlighted warning "Expected type 'A', got 'B' instead" when using the equality operator.

What have I tried? Of course, the first thing I tried was a type-mismatched equality operator and PyCharm gave no feedback. Next, I considered using function calls. As mentioned, function calls exhibit the desired behaviour already, so I tried overriding the __eq__(). When I use the dunder directly (a.__eq__(b)) then I see the warning I want, however if I just perform the operator in the usual fashion...no dice.

Here is a screen shot of the code showing the warning only on the dunder:enter image description here

Robino
  • 4,530
  • 3
  • 37
  • 40
  • Neither `p` nor `q` is going to be in the `__dict__` attribute of an instance; they are *class* attributes. – chepner Jun 26 '20 at 12:50
  • Python is not a compiled language and therefore has no compile time. Due to the dynamic nature of the language all type checks would have to be done at runtime. But keep in mind that Python is designed to be duck typed: what supports the interface is fine regarding the type. – Klaus D. Jun 26 '20 at 12:55
  • 1
    `mypy`, at least, won't even let you specify `A` as the type of `other`, as it conflicts with the type hint in `object.__eq__`. – chepner Jun 26 '20 at 12:56
  • 1
    @Klaus Python doesn't do *any* type checking at all; but 3rd party tools fill that gap and *do* do static type analysis. OP is asking how to get such static type checking for operators… – deceze Jun 26 '20 at 12:57
  • @KlausD. You say that "all type checks...have to be done at runtime" yet the screenshot clearly shows a check being done by the IDE, not at runtime, which seems to contradict your statement. – Robino Jun 26 '20 at 14:50
  • @chepner My apologies that the code was not cleaner - please ignore these artefacts as they are irrelevant to my question and I should have taken them out. – Robino Jun 26 '20 at 14:51
  • @chepner I shall check out mypy - cheers for the pointer! – Robino Jun 26 '20 at 14:53
  • @deceze Thank you for pointing out the correct terminology: **static type checking for operators** – Robino Jun 26 '20 at 14:54
  • @Robino What you see in the IDE is not a real type check. It is an educated guess for the type. The type of an object can only be determined safely by actually running the code. Amd even then if will only cover the cases executed. – Klaus D. Jun 26 '20 at 16:29
  • @Klaus Sure, the *actual* type will only be known at actual runtime, but that is what type annotations and static type checking are for: catching the most egregious errors by declaring what types you think things should have and ensuring basic sanity. You make it sound like that has no purpose whatsoever. – deceze Jun 26 '20 at 17:39

0 Answers0