Python Forum
Type hinting style and PEP8 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Type hinting style and PEP8 (/thread-26578.html)



Type hinting style and PEP8 - hlovatt - May-06-2020

Hi,

I was wondering if there was a preferred style for type hinting. In particular inline:

def process(self, items: Iterator[T], items2: Iterator[T]) -> Iterator[T]:
or on a seperate line via @overload:

@overload
def process(self, items: Iterator[T], items2: Iterator[T]) -> Iterator[T]: ...
def process(self, items, items2):
There is obviously pros and cons; e.g. 1st form is shorter, but 2nd reads more like traditional Python and is less cluttered making it easier to see the arguments.

Also I'm using PyCharm and its PEP8 warning wants an added space, i.e. PyCharm wants:

@overload
def process(self, items: Iterator[T], items2: Iterator[T]) -> Iterator[T]: ...

def process(self, items, items2):
Should PEP8 be modified to allow no blank line for overloads? PEP8 considerable pre-dates type hints! The no-blank for overloads style is what typeshed uses. Also typeshed uses the inline style unless the function actually requires @overload to type it correctly.

Thanks in advance for any advice,

Howard.


RE: Type hinting style and PEP8 - micseydel - May-06-2020

(May-06-2020, 04:26 AM)hlovatt Wrote: I was wondering if there was a preferred style for type hinting
As you mentioned, PEP 8 predates type hinting, so it doesn't address it. Personally, I try to avoid extra things, so I'd go with the single-line approach.

(May-06-2020, 04:26 AM)hlovatt Wrote: Should PEP8 be modified to allow no blank line for overloads?
We could certainly have a discussion about that, but ultimately we don't make those decisions.


RE: Type hinting style and PEP8 - hlovatt - May-07-2020

Thanks for your thoughts. I will keep experimenting with the different options.