fight mypy[1]

The following error will be reported without if:

  error: Name "tomllib" already defined (by an import)

While this can be silenced by a "# type: ignore", in some case[2] mypy
will report the following error:

  error: Unused "type: ignore" comment

[1]: https://github.com/python/mypy/issues/1153
[2]: https://github.com/lilydjwg/nvchecker/actions/runs/4916840821/jobs/8793454970
This commit is contained in:
lilydjwg 2023-05-09 13:31:11 +08:00
parent 62a3f336b8
commit af77af34f3
3 changed files with 23 additions and 12 deletions

View file

@ -12,6 +12,7 @@ import argparse
from typing import ( from typing import (
Tuple, NamedTuple, Optional, List, Union, Tuple, NamedTuple, Optional, List, Union,
cast, Dict, Awaitable, Sequence, Any, cast, Dict, Awaitable, Sequence, Any,
TYPE_CHECKING,
) )
import types import types
from pathlib import Path from pathlib import Path
@ -22,10 +23,13 @@ import json
import structlog import structlog
try: if TYPE_CHECKING:
import tomllib import tomli as tomllib
except ModuleNotFoundError: else:
import tomli as tomllib # type: ignore try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import platformdirs import platformdirs

View file

@ -14,10 +14,13 @@ from pathlib import Path
import contextvars import contextvars
import abc import abc
try: if TYPE_CHECKING:
import tomllib import tomli as tomllib
except ModuleNotFoundError: else:
import tomli as tomllib # type: ignore try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import structlog import structlog

View file

@ -5,11 +5,15 @@ import asyncio
import structlog import structlog
import os import os
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING
try: if TYPE_CHECKING:
import tomllib import tomli as tomllib
except ModuleNotFoundError: else:
import tomli as tomllib # type: ignore try:
import tomllib
except ModuleNotFoundError:
import tomli as tomllib
import pytest import pytest
import pytest_asyncio import pytest_asyncio