bamboo-cms 引入 requests 之后无法通过 mypy 的检查

报错如下,提示需要安装 types-requests,我使用 pdm add -d types-requests 安装之后报错依然如下:

(bamboo-3.12) ➜  bamboo git:(feat/ssg) ✗ pdm run pre-commit run --all-files
trim trailing whitespace.................................................Passed
fix end of files.........................................................Passed
debug statements (python)................................................Passed
ruff.....................................................................Passed
ruff-format..............................................................Passed
mypy.....................................................................Failed
- hook id: mypy
- exit code: 1

backend/bamboo/ssg.py:11: error: Library stubs not installed for "requests"  [import-untyped]
backend/bamboo/ssg.py:11: note: Hint: "python3 -m pip install types-requests"
backend/bamboo/ssg.py:11: note: (or run "mypy --install-types" to install all missing stub packages)
backend/bamboo/ssg.py:11: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
Found 1 error in 1 file (checked 30 source files)

我的 pyproject.toml:


[project]
name = "bamboo"
version = "0.1.0"
description = "A CMS optimized for conference hosting"
authors = [
    {name = "Bamboo Team", email = "me@frostming.com"},
]
dependencies = [
    "apiflask>=2.1.0",
    "flask-sqlalchemy>=3.1.1",
    "flask-migrate>=4.0.5",
    "python-dotenv>=1.0.0",
    "requests>=2.31.0",
    "Flask-APScheduler>=1.13.1",
]
requires-python = ">=3.12"
license = {text = "BSD 3-Clause"}

[tool.pdm]
package-type = "application"

[tool.pdm.scripts]
serve = { cmd = "flask run", help = "Run the development server" }
create-tables = { cmd = "flask create-tables", help = "Create tables" }
drop-tables = { cmd = "flask drop-tables", help = "Drop tables" }

[tool.pdm.dev-dependencies]
test = [
    "pytest>=7.4.3",
    "requests-mock>=1.11.0",
]
dev = [
    "pre-commit>=3.6.0",
    "flask-shell-ipython>=0.5.1",
    "types-requests>=2.31.0.10",
]

[tool.ruff]
line-length = 100
extend-select = [
  "I",    # isort
  "B",    # flake8-bugbear
  "C4",   # flake8-comprehensions
  "PGH",  # pygrep-hooks
  "RUF",  # ruff
  "W",    # pycodestyle
  "YTT",  # flake8-2020
]
extend-ignore = ["B018", "B019"]
src = ["src"]
exclude = ["tests/fixtures"]
target-version = "py312"

[tool.ruff.mccabe]
max-complexity = 10

@frostming

precommit 是把每个钩子装到自己的隔离环境中去,你 pdm add 显然没用

要在 precommit hook 里配置 additional_dependencies

干脆,也别用 requests 了,用 httpx 自带类型不用额外依赖

1 个赞

好的,谢谢,明白了,我是考虑到 requests 大伙儿更熟悉,换成 httpx 也行。