python的自动代码检查不正确

时间:2019-01-18 10:53:04

标签: python error-checking

我尝试使用特殊配置检查我的代码 .pre-commit-config.yaml     仓库:

  • 回购:https://github.com/ambv/black rev:“稳定” 钩子:

    • id:黑色 args:[-py36,-line-length = 120,src / webhook_app] python版本:python3.6
  • 回购:https://github.com/pre-commit/mirrors-mypy 修订版:v0.630 钩子:

    • id:mypy args:[--no-strict-optional,-ignore-missing-imports]
  • 回购:https://github.com/pre-commit/pygrep-hooks 版本:v1.1.0 钩子:

    • id:python-use-type-annotations
  • 回购:https://github.com/pre-commit/mirrors-pylint 版本:v1.9.1 钩子:

    • id:pylint
        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'```
    
  • 回购:https://github.com/pre-commit/pre-commit-hooks 版本:v1.4.0 钩子:

    • id:flake8 args:['-max-line-length = 120']
    • id:尾随空白

我开始检查 我的代码

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

能请你帮我吗?

1 个答案:

答案 0 :(得分:3)

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

Base = declarative_base(cls=DeferredReflection)

class MyClass(Base):
    ...

Base = declarative_base()

class MyClass(DeferredReflection, Base):
    ...