Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on a tricky overloaded Protocol #17206

Closed
Suor opened this issue May 2, 2024 · 0 comments · Fixed by #17348
Closed

Crash on a tricky overloaded Protocol #17206

Suor opened this issue May 2, 2024 · 0 comments · Fixed by #17348
Labels
crash topic-paramspec PEP 612, ParamSpec, Concatenate

Comments

@Suor
Copy link

Suor commented May 2, 2024

Crash Report

A crash happens while using _W wrapper below, which is a cut down version of functools._Wrapped.

Traceback

_broken.pyi:13: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.10.0
Traceback (most recent call last):
  File "mypy/checker.py", line 589, in accept
  File "mypy/nodes.py", line 586, in accept
  File "mypy/checker.py", line 626, in visit_overloaded_func_def
  File "mypy/checker.py", line 656, in _visit_overloaded_func_def
  File "mypy/checker.py", line 767, in check_overlapping_overloads
  File "mypy/checker.py", line 7813, in is_unsafe_overlapping_overload_signatures
  File "mypy/subtypes.py", line 1521, in is_callable_compatible
  File "mypy/subtypes.py", line 1862, in unify_generic_callable
  File "mypy/constraints.py", line 311, in infer_constraints
  File "mypy/constraints.py", line 407, in _infer_constraints
  File "mypy/types.py", line 1444, in accept
  File "mypy/constraints.py", line 802, in visit_instance
  File "mypy/constraints.py", line 314, in infer_constraints
  File "mypy/constraints.py", line 407, in _infer_constraints
  File "mypy/types.py", line 1730, in accept
  File "mypy/constraints.py", line 698, in visit_parameters
RuntimeError: Parameters cannot be constrained to
_broken.pyi:13: : note: use --pdb to drop into pdb

_broken.pyi:

from typing import Callable, Generic, ParamSpec, Protocol, TypeVar, overload

_PD = ParamSpec('_PD')
_PF = ParamSpec('_PF')
_RD = TypeVar('_RD')
_RF = TypeVar('_RF')

# from functools import _Wrapped as _W
class _W(Generic[_PF, _RF, _PD, _RD]):
    ...

class _DecoOrFab(Protocol[_PD, _RD]):
    @overload
    def __call__(self, f: Callable[_PF, _RF]) -> _W[_PF, _RF, ..., _RD]: ...
    @overload
    def __call__(self, **kwargs) -> _W[_PD, _RD, ..., ...]: ...

Your Environment

  • Mypy version used: 1.10.0
  • Mypy command-line flags: mypy --show-traceback _broken.pyi
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.11.8
  • Operating system and version: Linux, 6.6.26-1-MANJARO, x86_64 GNU/Linux

mypy.ini:

[mypy]
python_version = 3.11
check_untyped_defs = true
; disallow_untyped_calls = true
ignore_errors = false
ignore_missing_imports = true
warn_unused_ignores = true
warn_redundant_casts = true
warn_unused_configs = true
follow_imports = silent
disable_error_code = var-annotated
@Suor Suor added the crash label May 2, 2024
@ilevkivskyi ilevkivskyi added the topic-paramspec PEP 612, ParamSpec, Concatenate label Jun 3, 2024
ilevkivskyi added a commit that referenced this issue Jun 10, 2024
Moving towards #15907
Fixes #17206

This PR enables polymorphic inference during unification. This will
allow us to handle even more tricky situations involving generic
higher-order functions (see a random example I added in tests).
Implementation is mostly straightforward, few notes:
* This uncovered another issue with unions in solver, unfortunately
current constraint inference algorithm can sometimes infer weird
constraints like `T <: Union[T, int]`, that later confuse the solver.
* This uncovered another possible type variable clash scenario that was
not handled properly. In overloaded generic function, each overload
should have a different namespace for type variables (currently they all
just get function name). I use `module.some_func#0` etc. for overloads
namespaces instead.
* Another thing with overloads is that the switch caused unsafe overlap
check to change: after some back and forth I am keeping it mostly the
same to avoid possible regressions (unfortunately this requires some
extra refreshing of type variables).
* This makes another `ParamSpec` crash to happen more often so I fix it
in this same PR.
* Finally this uncovered a bug in handling of overloaded `__init__()`
that I am fixing here as well.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash topic-paramspec PEP 612, ParamSpec, Concatenate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants