Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type hinting style and PEP8
#1
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.
Reply
#2
(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.
Reply
#3
Thanks for your thoughts. I will keep experimenting with the different options.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python, question regarding PEP8 AthertonH 3 1,456 Apr-14-2022, 06:49 AM
Last Post: buran
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 5,230 Dec-20-2020, 04:23 AM
Last Post: adifrank
  Type hints and style MaxPowers 1 1,829 Feb-19-2020, 06:56 PM
Last Post: micseydel
  Type hinting - return type based on parameter micseydel 2 2,425 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Visual Studio Code - PEP8 Lambda Issue Qui_Ten 1 2,698 Jan-28-2019, 08:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020