Search code examples
pythonerror-checkingpre-commit.com

Automatic code checking for python works incorrect


I try to check my code using special configuration .pre-commit-config.yaml repos:

-   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
    

I start my checking my code

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

I got an error from hookid: python-use-type-annotations

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

When I remove # type: ignore python-use-type-annotations says that everything is ok but mypy sends me an error Invalid base class.

Could you please help me?


Solution

  • I think you're not using declarative_base() properly.

    Base = declarative_base(cls=DeferredReflection)
    
    class MyClass(Base):
        ...
    

    or

    Base = declarative_base()
    
    class MyClass(DeferredReflection, Base):
        ...