[Python-ideas] PEP 8 update on line length

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Tue Feb 19 10:13:06 EST 2019


On 2/19/19 8:28 AM, Samuel Colvin wrote:

 > Let's take a real life example from here
 > 
<https://github.com/samuelcolvin/pydantic/blob/master/pydantic/utils.py#L246>
 > (changed very slightly for this example), in tranitional python the
 > signature might be:
 >
 > def resolve_annotations(raw_annotations, module_name):
 >
 > 55 characters, all fine. Now let's look at that same function in
 > modern python with type hints:
 >
 > def resolve_annotations(*, raw_annotations: Dict[str, Type[Any]], 
module_name: Optional[str]) -> Dict[str, Type[Any]]:

If it were me, I'd probably write (or would have re-written when I added
the type hints) that as follows:

     def resolve_annotations(
             *,
             raw_annotations: Dict[str, Type[Any]],
             module_name: Optional[str]
         ) -> Dict[str, Type[Any]]:

which leaves the longest line at 46 characters, even shorter than your
original 55.

And if the type hints are long, then a similar approach can mitigate
that:

     def resolve_annotations(
             *,
             raw_annotations:
                 Dict[str, Type[Any]],
             module_name:
                 Optional[str]
         ) -> Dict[str, Type[Any]]:

 > Type hints (for good or bad depending on your view point) have made
 > python code more verbose, that's just a fact. Code that used to fit on
 > one line with 80char limit now require 120 (or more), therefore in my
 > opinion the recommendation should change to keep up with changes in
 > python.

Nothing says that you have to write an entire function header on one
line.


More information about the Python-ideas mailing list