PEP8 插件破坏了我在 Sublime 3 中的类型提示代码 [英] PEP8 plugin breaks my Type Hint code in Sublime 3

查看:45
本文介绍了PEP8 插件破坏了我在 Sublime 3 中的类型提示代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不在保存设置时禁用格式,因为 Python PEP8 Autoformat 插件重新格式化我的代码,导致语法错误.

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
}

推荐答案

你的 linter 听起来有点过时了,因为它既不识别 walrus operator := 或您的类型注释.查看插件的 Package Control 页面,您可以在顶部看到它说MISSING",这意味着源代码存储库消失了,很可能是因为它不再被维护.该软件包最后一次修改是在 5 年前,并且最近没有安装,因此有非常有力的证据表明它已经死了.

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.

作为替代插件,我强烈推荐 Anaconda(与 Anaconda Python 发行版无关).它运行良好(大多数情况下),正在积极开发中,经常更新、修复错误和新功能,并执行代码完成和代码智能以及 linting/自动格式化.该网站完成了您需要做的所有配置,以及如何关闭和打开不同的功能.有多种不同的 linting/格式化选项可供选择,包括 AutoPEP8、PyFlakes 和 PyLint.我真的很喜欢.

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

这篇关于PEP8 插件破坏了我在 Sublime 3 中的类型提示代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆