python - python 的自动代码检查工作不正确

标签 python error-checking pre-commit.com

我尝试使用特殊配置检查我的代码 .pre-commit-config.yaml repo 协议(protocol):

-   repo: https://github.com/ambv/black
    rev: 'stable'
    hooks:
    - id: black
      args: [--py36, --line-length=120, src/webhook_app]
      python-version: python3.6

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.630
    hooks:
    - id: mypy
      args: [--no-strict-optional, --ignore-missing-imports]

-   repo: https://github.com/pre-commit/pygrep-hooks
    rev: v1.1.0
    hooks:
    -   id: python-use-type-annotations

-   repo: https://github.com/pre-commit/mirrors-pylint
    rev: v1.9.1
    hooks:
    - id: pylint
      ```#args: [--disable=all, --enable=classes]
          args: [
            --max-line-length=120,
            --disable=design,
            --disable=missing-docstring,
            --disable=bad-continuation,
            --disable=max-module-lines,
            --disable=useless-super-delegation,
            --disable=import-error,
            --disable=logging-fstring-interpolation,
            --disable=invalid-name,
            --disable=duplicate-code
          ]
          # exclude tests folder and manage.py to be checked
          exclude: 'tests|manage.py'```

-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v1.4.0
    hooks:
    - id: flake8
      args: ['--max-line-length=120']
    - id: trailing-whitespace
    

我开始检查 我的代码

import warnings
from contextlib import contextmanager
from enum import Enum

from sqlalchemy import create_engine
from sqlalchemy import exc
from sqlalchemy.ext.declarative import DeferredReflection, declarative_base
from sqlalchemy.inspection import inspect
from sqlalchemy.orm import scoped_session, sessionmaker

import structlog


logger = structlog.get_logger(__name__)


class Base(DeferredReflection, declarative_base()): # type: ignore

    """ Abstract base class for construction of mappings
    based on a deferred reflection step.
    All classes inherited from it will be reflected by the time
    of calling DeferredReflection.prepare()""" 
  
    __abstract__ = True

    def __repr__(self):
        inspector = inspect(self.__table__)
        return "{}({})".format(
            self.__class__.__name__,
            ", ".join("%s=%s" % (column.name, self.__dict__[column.name]) for column in inspector.columns),
        )

我收到来自 hookid: python-use-type-annotations 的错误

src/webhook_app/models.py:17:class Base(DeferredReflection, declarative_base()):  # type: ignore

当我删除# type: ignorepython-use-type-annotations说一切正常,但 mypy 向我发送错误 Invalid base class .

你能帮我一下吗?

最佳答案

我认为您没有正确使用declarative_base()

Base = declarative_base(cls=DeferredReflection)

class MyClass(Base):
    ...

Base = declarative_base()

class MyClass(DeferredReflection, Base):
    ...

关于python - python 的自动代码检查工作不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54252459/

相关文章:

c - 为什么匿名枚举不符合 MISRA C 2012 规则 10.3 而命名枚举不符合?

java - 在 Intellij IDEA 中禁用实时错误检查?

django - 预提交 Hook 以检查 django 迁移

python - 使用 mysqldump 执行 mySQL 备份时权限被拒绝

python - 递归调用的问题

python - 如何在 python3.3 中使 range() 只有偶数?

algorithm - 错误恢复算法?

python - 如何将列表随机分成n个几乎相等的部分?

yaml - 预提交检查 yaml 错误 : "expected a single document in the stream"

git - 为什么 git end-of-file-fixer 不断在我的文件末尾添加未知字符?