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

View File

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

View File

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