PEP8 plugin breaks my Type Hint code in Sublime 3

473 Views Asked by At

I had to disable the format on save setting, because Python PEP8 Autoformat plugin reformatted my code, causing a syntax error.

My code (focus on last lines):

from typing import List, Tuple
from my_enent import MyEvent


def my_preprocessor(raw_event, context: object, env: MyEnv) \
        -> Tuple[dict, VideoFreezeEvent]:
    if isinstance(raw_event, dict) and 'Output' in raw_event:
        # comments
        raw_state_machine_event = json.loads(raw_state_machine_event['Output'])
    # comments
    parallel_outputs = raw_state_machine_event.get(
        'my_data').get('parallel_outputs')
    if len(parallel_outputs) > 0:
        state_machine_event = parallel_outputs[0]
        my_list: List[MyEvent] = [
            my_util.populate_dataclass(MyEvent, event)
            for event in parallel_outputs
        ]
        another_event = events_list[0]

After the plugin reformats the code, the relevant part of the code that causes the syntax error becomes:

   if len(parallel_outputs) > 0:
       state_machine_event = parallel_outputs[0]
       my_list:
           List[MyEvent] = [
               my_util.populate_dataclass(MyEvent, event)
               for event in parallel_outputs
           ]
       another_event = events_list[0]

How can I prevent/teach the plugin to not break this code please?


Some package settings that might be the way through, if a passage exists in the first place:

{
    // list codes for fixes; used by --ignore and --select
    "list-fixes": false,

    // do not fix these errors / warnings (e.g. [ "E501" , "E4" , "W"])
    "ignore": [],

    // select errors / warnings (e.g. ["E4", "W"])
    "select": [],

    // Maximum line length
    "max-line-length": 79
}
3

There are 3 best solutions below

4
On BEST ANSWER

Your linter sounds like it is rather out of date, as it neither recognizes the walrus operator := or your type annotations. Looking at the plugin's Package Control page, you can see that up at the top it says "MISSING", which means the source code repo is gone, most likely because it's not being maintained anymore. The package was last modified 5 years ago, and there are no recent installations, so there's very strong evidence it's dead.

As a replacement plugin, I'd highly recommend Anaconda (not related to the Anaconda Python distribution). It works great (mostly), is under active development with frequent updates, bugfixes, and new features, and does code completion and code intelligence along with linting/autoformatting. The website goes through all the configuration you need to do, and how to turn off and on the different features. There are several different linting/formatting options to choose from, including AutoPEP8, PyFlakes, and PyLint. I really like it.

(And no, I'm not associated with it or its author in any way.)

1
On

I installed Black via pip, and used sublack Sublime plugin, which appears to be running smoothly.

Anaconda Sublime plugin suggested by MattDMo is cool, but a bit slow (with the default settings at least), any my Mac laptop is fairly new.

0
On

I found workaround. Go to plugin settings (Preferences -> Package Settings -> Python PEP8 Autoformat -> …), add ignore rule, e.g.:

{
  // Workaround for typing hints
  "ignore": ["E701"],
}

I guess it ignores this warning: https://www.flake8rules.com/rules/E701.html Seems not very harmful.