diff options
-rw-r--r-- | .github/workflows/run-on-pr.yaml | 31 | ||||
-rw-r--r-- | .github/workflows/run-test.yaml | 31 | ||||
-rw-r--r-- | alembic/context.pyi | 8 | ||||
-rw-r--r-- | docs/build/unreleased/897.rst | 6 | ||||
-rw-r--r-- | docs/build/unreleased/900.rst | 5 | ||||
-rw-r--r-- | tools/write_pyi.py | 18 | ||||
-rw-r--r-- | tox.ini | 2 |
7 files changed, 93 insertions, 8 deletions
diff --git a/.github/workflows/run-on-pr.yaml b/.github/workflows/run-on-pr.yaml index 4943b3e..6a90b03 100644 --- a/.github/workflows/run-on-pr.yaml +++ b/.github/workflows/run-on-pr.yaml @@ -48,3 +48,34 @@ jobs: - name: Run tests run: tox -e py-${{ matrix.sqlalchemy }} + + run-mypy: + name: mypy-${{ matrix.python-version }}-${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - "ubuntu-latest" + python-version: + - "3.9" + + fail-fast: false + + steps: + - name: Checkout repo + uses: actions/checkout@v2 + + - name: Set up python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + architecture: ${{ matrix.architecture }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install --upgrade tox setuptools + pip list + + - name: Run mypy + run: tox -e mypy diff --git a/.github/workflows/run-test.yaml b/.github/workflows/run-test.yaml index 467d810..7105a8a 100644 --- a/.github/workflows/run-test.yaml +++ b/.github/workflows/run-test.yaml @@ -56,3 +56,34 @@ jobs: - name: Run tests run: tox -e py-${{ matrix.sqlalchemy }} + + run-mypy: + name: mypy-${{ matrix.python-version }}-${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - "ubuntu-latest" + python-version: + - "3.9" + + fail-fast: false + + steps: + - name: Checkout repo + uses: actions/checkout@v2 + + - name: Set up python + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + architecture: ${{ matrix.architecture }} + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install --upgrade tox setuptools + pip list + + - name: Run mypy + run: tox -e mypy diff --git a/alembic/context.pyi b/alembic/context.pyi index d789a22..5c29d3a 100644 --- a/alembic/context.pyi +++ b/alembic/context.pyi @@ -13,8 +13,10 @@ if TYPE_CHECKING: from sqlalchemy.engine.base import Connection from sqlalchemy.sql.schema import MetaData - from .migration import MigrationContext + from .config import Config from .runtime.migration import _ProxyTransaction + from .runtime.migration import MigrationContext + from .script import ScriptDirectory ### end imports ### @@ -62,6 +64,8 @@ def begin_transaction() -> Union["_ProxyTransaction", ContextManager]: """ +config: Config + def configure( connection: Optional["Connection"] = None, url: Optional[str] = None, @@ -715,6 +719,8 @@ def run_migrations(**kw) -> None: """ +script: ScriptDirectory + def static_output(text): """Emit text directly to the "offline" SQL stream. diff --git a/docs/build/unreleased/897.rst b/docs/build/unreleased/897.rst new file mode 100644 index 0000000..9ae0492 --- /dev/null +++ b/docs/build/unreleased/897.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, mypy + :tickets: 897 + + Fixed an import in one of the .pyi files that was triggering an + assertion error in some versions of mypy. diff --git a/docs/build/unreleased/900.rst b/docs/build/unreleased/900.rst new file mode 100644 index 0000000..88f9f17 --- /dev/null +++ b/docs/build/unreleased/900.rst @@ -0,0 +1,5 @@ +.. change:: + :tags: bug, typing + :tickets: 900 + + Added missing attributes from context stubs. diff --git a/tools/write_pyi.py b/tools/write_pyi.py index 61c3052..60728a8 100644 --- a/tools/write_pyi.py +++ b/tools/write_pyi.py @@ -9,7 +9,7 @@ from mako.pygen import PythonPrinter sys.path.append(str(Path(__file__).parent.parent)) -if True: # avoid flake/zimports missing with the order +if True: # avoid flake/zimports messing with the order from alembic.operations.base import Operations from alembic.runtime.environment import EnvironmentContext from alembic.script.write_hooks import console_scripts @@ -19,11 +19,9 @@ if True: # avoid flake/zimports missing with the order IGNORE_ITEMS = { "op": {"context", "create_module_class_proxy"}, "context": { - "config", "create_module_class_proxy", "get_impl", "requires_connection", - "script", }, } @@ -36,6 +34,13 @@ def generate_pyi_for_proxy( ignore_output: bool, ignore_items: set, ): + if sys.version_info < (3, 9): + raise RuntimeError("This script must be run with Python 3.9 or higher") + + # When using an absolute path on windows, this will generate the correct + # relative path that shall be written to the top comment of the pyi file. + if Path(progname).is_absolute(): + progname = Path(progname).relative_to(Path().cwd()).as_posix() imports = [] read_imports = False @@ -88,7 +93,8 @@ def generate_pyi_for_proxy( def _generate_stub_for_attr(cls, name, printer): - printer.writeline(f"{name}: Any") + type_ = cls.__annotations__.get(name, "Any") + printer.writeline(f"{name}: {type_}") def _generate_stub_for_meth(cls, name, printer): @@ -154,15 +160,15 @@ def run_file( else: with NamedTemporaryFile(delete=False, suffix=".pyi") as f: f.close() + f_path = Path(f.name) generate_pyi_for_proxy( cls_to_generate, progname, source_path=source_path, - destination_path=f.name, + destination_path=f_path, ignore_output=True, ignore_items=ignore_items, ) - f_path = Path(f.name) sys.stdout.write(f_path.read_text()) f_path.unlink() @@ -66,7 +66,7 @@ deps= mako types-pkg-resources types-python-dateutil - # is imported in alembic/testing and mypy complains if it's installed. + # is imported in alembic/testing and mypy complains if it's not installed. pytest commands = mypy ./alembic/ --exclude alembic/templates |