mypy icon indicating copy to clipboard operation
mypy copied to clipboard

There is a bug in `exclue` ?

Open touale opened this issue 1 year ago • 3 comments

I tried to use mypy in tox, and despite the exclude api, it seems unable to skip the relevant directories. Also, I tried to only use command mypy src/xxx_service, but it actually detected src/lib.

# tox running:

lint: commands[0]> mypy src/xxxxx_service tests
src/libs/xxxxxx/xxxxxx/text/__init__.py:3: error: Name "symbols" is not defined  [name-defined]
src/libs/xxxxxx/xxxxxx/text/cleaner.py:53: error: Missing positional argument "language" in call to "clean_text"  [call-arg]


lint: commands[0]> mypy src/oral_training_service tests --exclude src/libs
src/libs/xxxxxx/xxxxxx/text/__init__.py:3: error: Name "symbols" is not defined  [name-defined]
src/libs/xxxxxx/xxxxxx/text/cleaner.py:53: error: Missing positional argument "language" in call to "clean_text"  [call-arg]

It should not detect the lib directory.

Then, I tried to use it in execute directly in the terminal. It won't have any problems

$ mypy src/xxxxxx_service tests
Success: no issues found in 37 source files

tox.ini :

[tox]
envlist = py310,lint,pack
isolated_build = True

[testenv]
deps =
    poetry
setenv =
    CHECK_DIRS=src/xxxxxx_service tests
    PIP_INDEX_URL = http://172.22.121.51:8081/repository/pypi-public/simple
install_command = pip install --trusted-host 172.22.121.51 {opts} {packages}
commands =
;    poetry run pytest tests

[testenv:style]
deps = ruff
commands =
    ruff check {env:CHECK_DIRS} --fix
    ruff format {env:CHECK_DIRS}

[testenv:lint]
deps =
    mypy
commands =
    mypy {env:CHECK_DIRS}

[testenv:pack]
commands =
    poetry build


pyproject.toml:


...

[tool.poetry.dependencies]
python = "^3.10"
...

[tool.poetry.group.dev.dependencies]
ruff = "^0.4.6"
coverage = ">=7.2,<8.0"
pytest = ">=7.3,<8.0"
pytest-cov = ">=4.0,<5.0"
pytest-runner = ">=6.0,<7.0"
pytest-asyncio = ">=0.21,<1.0"
werkzeug = ">=2.3,<3.0"
httpx = ">=0.24,<1.0"
tox = "^4.15.0"
pre-commit = "^3.7.1"
mypy = "^1.10.0"

[[tool.poetry.packages]]
include = "*"
from = "src/libs/xxxx/xxxxxx"

[[tool.poetry.packages]]
include = "xxxxxxxx"
from = "src"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.pytest]
doctest_optionflags = ["NORMALIZE_WHITESPACE", "ELLIPSIS", "NUMBER"]

[tool.pytest.ini_options]
testpaths = "tests"
....


[tool.coverage.run]
omit = [
    "src/libs/*"
]

[tool.ruff]
target-version = "py310"
line-length = 119
exclude = [
    "src/libs/*"
]

[tool.ruff.lint]
extend-select = [
    "C", # Complexity
    "E", # PEP8 errors
    "F", # PEP8 formatting
    "I", # Import sorting
    "UP", # Pyupgrade upgrades
    "W", # PEP8 warnings
]
ignore = [
    "C901", # Function too complex
    "E501", # Line length (handled by ruff-format)
    "UP007", # X | Y style Unions
]


[tool.mypy]
exclude = [
    "src/libs/*",
    "src/libs/xxxxxx"
]
ignore_missing_imports = true
check_untyped_defs = true
disallow_untyped_defs = false

version: python 3.10 mypy 1.10.0 (compiled: yes) tox 4.15.0


doubt:

  • Is it caused by [[tool.poetry.packages]]?
  • Is it caused by tox? (only have problem when running in tox)
  • Is it caused by python code,some of the src/xxxxxx_service code calls libs function.

touale avatar Jun 12 '24 12:06 touale

So, I've been experiencing this myself with Sylva. Exact same situation. Tests run as expected from both within the venv and from normal shells, but all my supposedly excluded directories were being scanned as well when ran with tox.

Despite the tox env using skip-install, something must be happening that mypy begins following imports as if it were installed and ran. Still not 100% sure what the underlying difference is here, but after quite a bit of fighting with it, I've found a fix.

Add follow_imports = "skip" to your mypy section. (edit inserted by mypy maintainer: you probably don't want to do this, and if you don't understand what it does, you certainly don't want to do this) (edit inserted by me: ^ that's right it's a pretty poor solution. just the only one that was remotely functional at the time and should only be used as an absolute last resort. I've since found this change to be a much more suitable fix without needing to use skip. Still not perfect, but much better. Note that tox is now installing in an editable mode rather than prod mode to fix relative path exclusion.)

Curious if anyone knows what's leading to this being necessary.

ppfeister avatar Aug 27 '24 06:08 ppfeister

I should note that skip is highly frowned upon in the documentation, but it makes everything else work as expected in my use case and only so far. I'm interested in hearing other people's input as to the pros and cons here and if there are alternatives that allow normal use of follow imports.

ppfeister avatar Aug 27 '24 06:08 ppfeister

@ppfeister Thank you for your answer. In fact, I have given up the import of tox.

touale avatar Aug 27 '24 07:08 touale