Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python class members type hinting

Tags:

I am using PyCharm to develop some Python app. I am trying to use as cutting-edge python as possible, so I am making use of new python features. I have a problem with type hinting.

Let's all take a look at my DataAnalyzer class:

class DataAnalyzer:

    def __init__(self, train_data: pd.DataFrame, test_data: pd.DataFrame) -> None:
        self.train_data = train_data
        self.test_data = test_data

    def analyze(self):
        pass

Now PyCharm spams me with yellow bulbs wanting me to add type annotations to self.train_data and self.test_data. If I click this message to do so, PyCharm adds two lines at the beginning of my class:

class DataAnalyzer:
    train_data: pd.DataFrame
    test_data: pd.DataFrame

    def __init__(self, train_data: pd.DataFrame, test_data: pd.DataFrame) -> None:
        self.train_data = train_data
        self.test_data = test_data

    def analyze(self):
        pass

I guess now it looks nicer, but AFAIK by writing those variables like this I make them static.

I thought about annotating the types like this:

class DataAnalyzer:

    def __init__(self, train_data: pd.DataFrame, test_data: pd.DataFrame) -> None:
        self.train_data: pd.DataFrame = train_data
        self.test_data: pd.DataFrame = test_data

    def analyze(self):
        pass

Which is definitely not clear, but I am not making my class members static, when I don't want to.

I know, that by having the types annotated in the method signature, doing this one more time when I just assign those, is an overkill, but I am asking for the general rule. Should I annotate those types like PyCharm suggests me to, or should I do this inline?

like image 560
dabljues Avatar asked Dec 22 '18 15:12

dabljues


People also ask

How do you type hinting in Python?

Here's how you can add type hints to our function: Add a colon and a data type after each function parameter. Add an arrow ( -> ) and a data type after the function to specify the return data type.

Should I use type hinting in Python?

Type hints work best in modern Pythons. Annotations were introduced in Python 3.0, and it's possible to use type comments in Python 2.7. Still, improvements like variable annotations and postponed evaluation of type hints mean that you'll have a better experience doing type checks using Python 3.6 or even Python 3.7.

Does Python enforce type hints?

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. This module provides runtime support for type hints. The most fundamental support consists of the types Any , Union , Callable , TypeVar , and Generic .

Does Python make hinting faster?

There's no effect on performance whatsoever with or without type hints. Cpython ignore type hints similar to how comments are discarded during runtime.


2 Answers

PyCharm's suggest is right. In fact, I think the following code is better:

class DataAnalyzer:
    train_data: pd.DataFrame
    test_data: pd.DataFrame

    def __init__(self, train_data, test_data):
        self.train_data = train_data
        self.test_data = test_data

    def analyze(self):
        pass

Explain:

  • Annotate a member does not make it static.
  • We should not annotate arguments in the __init__ function again.
  • -> None after __init__ can be omitted. For __init__ never return a value.

Thanks.

like image 65
胡玉新 Avatar answered Oct 02 '22 06:10

胡玉新


Both options are valid - check out pep 526

Type annotations can also be used to annotate class and instance variables in class bodies and methods. In particular, the value-less notation a: int allows one to annotate instance variables that should be initialized in __init__ or __new__.

The MyPy documentation is also a good resource for type hinting

like image 31
Alonme Avatar answered Oct 02 '22 07:10

Alonme