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

limit Literal function special case to only functions #329

Merged
merged 2 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Limit special case for `Literal` callables to
functions, not any callable (#329)
- Support for constants in stubs that do not exist
at runtime (#330)
- Fix detection of PEP 604 union types in stubs (#327)
Expand Down
8 changes: 5 additions & 3 deletions pyanalyze/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def function(x: int, y: list[int], z: Any):
import enum
import inspect
from itertools import chain
from types import FunctionType
import qcore
import textwrap
from typing import (
Expand Down Expand Up @@ -399,9 +400,10 @@ def get_type_value(self) -> Value:

def can_assign(self, other: Value, ctx: CanAssignContext) -> CanAssign:
# Make Literal[function] equivalent to a Callable type
signature = ctx.get_signature(self.val)
if signature is not None:
return CallableValue(signature).can_assign(other, ctx)
if isinstance(self.val, FunctionType):
signature = ctx.get_signature(self.val)
if signature is not None:
return CallableValue(signature).can_assign(other, ctx)
if isinstance(other, KnownValue):
if self.val is other.val:
return {}
Expand Down