python / python/mypy

Some `isinstance(obj, os.PathLike)` widen the type of `obj`

Đang mở
#16,985 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

bug
Ngôn ngữ chính
Python
Star
20.6k
Fork
3.3k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

Bug Report

Given an object of type Union[None, str, pathlib.Path] the revealed type before and after a check

if isinstance(obj, os.PathLike):
    # code

widens from the given type before to builtin.object afterwards.

To Reproduce

Unfortunately, I have not been able to make a minimal reproducer, since all my attempts have type checked fine.

We've encounted this in this commit of the PR https://github.com/systemd/mkosi/pull/2373. The codebase is stdlib only and targets Python 3.9 (because it is shipped in RHEL).

One of the two examples can be found in the function built_uki here. The incoming Context object is a dataclass that holds many things, but relevant for this issue is context.config.secure_boot_key, which has type Union[None, str, pathlib.Path]. This type is defined on the dataclass and mypy reveals it at the beginning of the function.

Adding a lot of reveal_type statements I could find that this line building a command line for a program

            reveal_type(context.config.secure_boot_key)
            if isinstance(context.config.secure_boot_key, os.PathLike):
                options += ["--ro-bind", context.config.secure_boot_key, context.config.secure_boot_key]
            reveal_type(context.config.secure_boot_key)

switches the revealed type:

mkosi/__init__.py:1906:25: note: Revealed type is "Union[pathlib.Path, builtins.str]"
mkosi/__init__.py:1909:25: note: Revealed type is "builtins.object"
mkosi/__init__.py:1931:38: error: List item 1 has incompatible type "object"; expected "Union[Path, str]"  [list-item]
                    "--pcr-private-key", context.config.secure_boot_key,
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 42 source files)

Looking further the changes in the commit, not all isinstance(context.config.secure_boot_key, os.PathLike) checks added there suffer from this problem.

The problem has been encountered with

$ mypy --version
mypy 1.8.0 (compiled: no)

but pyright reveals the proper type.

Expected Behavior

The type of the object does not change.

Actual Behavior

The type is widened to builtin.object

mkosi/__init__.py:1906:25: note: Revealed type is "Union[pathlib.Path, builtins.str]"
mkosi/__init__.py:1909:25: note: Revealed type is "builtins.object"
mkosi/__init__.py:1931:38: error: List item 1 has incompatible type "object"; expected "Union[Path, str]"  [list-item]
                    "--pcr-private-key", context.config.secure_boot_key,
                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in 1 file (checked 42 source files)

The problem can be worked around by changing the check

-if isinstance(context.config.secure_boot_key, os.PathLike):
+if context.config.secure_boot_key and not isinstance(context.config.secure_boot_key, str):

Your Environment

  • Mypy version used: 1.8.0
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): The following section in pyproject.toml is used to configure mypy:
[tool.mypy]
python_version = 3.9
# belonging to --strict
warn_unused_configs = true
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_untyped_decorators = true
disallow_incomplete_defs = true
check_untyped_defs = true
no_implicit_optional = true
warn_redundant_casts = true
warn_unused_ignores = false
warn_return_any = true
no_implicit_reexport = true
# extra options not in --strict
pretty = true
show_error_codes = true
show_column_numbers = true
warn_unreachable = true
allow_redefinition = true
strict_equality = true
  • Python version used: 3.11.7 on Arch Linux

I'm happy to provide more details

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện hành vi được báo cáo từ ví dụ built_uki trong mkosi/init.py, sử dụng mypy 1.8.0 và giá trị Union[None, str, pathlib.Path] được nêu. Theo dõi quá trình thu hẹp kiểu của isinstance và thêm một trường hợp hồi quy cho thấy kiểu này vẫn có thể được sử dụng như Union[Path, str] sau khi kiểm tra.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.