Skip to content

type inference inside of conditionally defined functions appears incorrect #259

@mikeshardmind

Description

@mikeshardmind

Summary

Callable[..., Any] & ~None becomes Callable[..., Any] | None inside a function defined within the branch that narrowed to include ~None

Minimal repro:

https://play.ty.dev/ba37c54e-a0ea-4382-87fb-e2f4945868bf

real world code:

https://github.com/mikeshardmind/async-utils/blob/7bf094f063a727901539598811d3fd907ccf5342/src/async_utils/task_cache.py#L153-L158

relevant errors on that real world code with reveal types added:

info: revealed-type: Revealed type
   --> src\async_utils\task_cache.py:243:9
    |
241 |     else:
242 |         from typing import reveal_type
243 |         reveal_type(cache_transform)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `((...) -> tuple[@Todo, dict[str, Any]]) & ~None`
244 |         def key_func(args: tuple[t.Any, ...], kwds: dict[t.Any, t.Any], /) -> Hashable:
245 |             reveal_type(cache_transform)
    |

info: revealed-type: Revealed type
   --> src\async_utils\task_cache.py:245:13
    |
243 |         reveal_type(cache_transform)
244 |         def key_func(args: tuple[t.Any, ...], kwds: dict[t.Any, t.Any], /) -> Hashable:
245 |             reveal_type(cache_transform)
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `((...) -> tuple[@Todo, dict[str, Any]]) | None`
246 |             return make_key(*cache_transform(args, kwds))
    |

error: lint:call-non-callable: Object of type `None` is not callable
   --> src\async_utils\task_cache.py:246:30
    |
244 |         def key_func(args: tuple[t.Any, ...], kwds: dict[t.Any, t.Any], /) -> Hashable:
245 |             reveal_type(cache_transform)
246 |             return make_key(*cache_transform(args, kwds))
    |                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
247 |
248 |     def wrapper[**P, R](coro: TaskCoroFunc[P, R], /) -> TaskFunc[P, R]:
    |
info: `lint:call-non-callable` is enabled by default

Version

0.0.0-alpha.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    narrowingrelated to flow-sensitive type narrowing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions