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.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply
#3
Thanks for your thoughts. I will keep experimenting with the different options.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 1,288 Dec-20-2020, 04:23 AM
Last Post: adifrank
  Tkinter font style ATARI_LIVE 2 501 Oct-24-2020, 08:22 PM
Last Post: ATARI_LIVE
  C style strings deanhystad 2 968 May-16-2020, 02:57 PM
Last Post: Larz60+
  How to generate a log in a list style? wagnergt12 5 1,073 Apr-22-2020, 12:47 PM
Last Post: buran
  Type hints and style MaxPowers 1 877 Feb-19-2020, 06:56 PM
Last Post: micseydel
  Type hinting - return type based on parameter micseydel 2 1,120 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Visual Studio Code - PEP8 Lambda Issue Qui_Ten 1 1,308 Jan-28-2019, 08:17 AM
Last Post: buran
  Is this good style? league55 3 1,726 Jan-14-2018, 04:25 PM
Last Post: Gribouillis
  pdf sms alternating conversation style sdcaliber 4 3,128 Apr-20-2017, 05:20 AM
Last Post: sdcaliber

Forum Jump:

User Panel Messages

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