Skip to content Skip to sidebar Skip to footer

Type Hinting Propagation For Overriding Methods

Is it possible to propagate type hinting to overridding methods? Say, I have the following classes: class Student: def study(): pass class Option: self.option_valu

Solution 1:

PyCharm does not look at the superclass type hints when overriding methods. I don't know if this is a bug or a feature, although I would be leaning toward the latter: Python does not require overriding methods to have the same signatures or accept the same types as the methods they override. Put differently, type hints on BaseChoice are not automatically valid for RationalChoice.

What PyCharm does do, and what seems to be confusing you, is take a quick guess and decide that Student would be a sensible class for a parameter called student. There is no class Options, so that heuristic fails.

So if you really, really want type hints, there's really no alternative to specifying them everywhere you want them.

If you are using Python 3, you could try the new in-language type hint (annotation) syntax:

classRationalChoice(BaseChoice):defmake_choice(self, student: Student, options: list):
        return

Post a Comment for "Type Hinting Propagation For Overriding Methods"