Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reuse python typing annotations

I like to create my own annotations or typing extensions in my project. Basically what I like to do is reuse type annotations across my project.

So, instead of typing List[Dict[str, pd.DataFrame]] hundred times, I want to save this to a python variable and reuse it that way. Sort of custom annotations.

How can we do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried NewType = List[Dict[str, pd.DataFrame]] but it doesnt work. In the sense that, the autocomplete features doesn’t come up suggesting the different functions/attributes for the object.

>Solution :

You can read about custom new types in here. It will help you more about this.

from typing import NewType, List, Dict
import pandas as pd

CustomType = NewType('CustomType',List[Dict[str, pd.DataFrame]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading