2

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
}
MattDMo
  • 100,794
  • 21
  • 241
  • 231
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    I've got no idea why the formatter wants to insert a line break there. Very strange. – Frank Yellin Oct 02 '20 at 19:26
  • @FrankYellin maybe something with the type hint, but I cannot figure it out either. – gsamaras Oct 02 '20 at 19:41
  • Does it put a newline after the colon in the walrus operator `:=`? If so, it's probably just putting newlines after all colons. – MattDMo Oct 02 '20 at 20:29
  • @MattDMo it does yes. Hmm I think this is an overgeneralization, because I haven't noticed this with list slicing for example. – gsamaras Oct 05 '20 at 08:35

3 Answers3

1

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.)

MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • So you mean to give up on Sublime 3? It's a fair point unfortunately, since what you say about PEP8 plugin is true, from my parallel investigation (sigh). Thanks! – gsamaras Oct 05 '20 at 20:12
  • 1
    @gsamaras No, not at all!!! The Anaconda plugin I mentioned is a plugin FOR Sublime. It's completely different from the Anaconda Python distribution. – MattDMo Oct 05 '20 at 21:13
  • sorry for my misunderstanding. Are you talking about [Anaconda for Sublime 3](http://damnwidget.github.io/anaconda/IDE/)? I am so excited to try it out! In any case, can you please edit your question to link to the Anaconda Sublime plugin you refer to? Just so everybody is on the same page. – gsamaras Oct 05 '20 at 21:58
  • 1
    @gsamaras There is a link there in the code block to its Package Control page, but I'll make it a little more noticeable. – MattDMo Oct 05 '20 at 22:20
1

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.

Dmitriy
  • 11
  • 1
0

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.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • So far the plugin works perfectly. Black however caused me a single issue with multiline values in a Python dictionary, read more in [pep8-multi-line-dict-with-multi-line-value](https://stackoverflow.com/questions/64286651/pep8-multi-line-dict-with-multi-line-value). But it was a rare case. – gsamaras Oct 09 '20 at 21:42