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

Preserve Annotated annotations on access to methods of literals #541

Merged
merged 1 commit into from
Sep 2, 2022
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

- Preserve `Annotated` annotations on access to methods of
literals (#541)
- `allow_call` callables are now also called if the arguments
are literals wrapped in `Annotated` (#540)
- Support Python 3.11 (#537)
Expand Down
10 changes: 10 additions & 0 deletions pyanalyze/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ def _get_attribute_from_known(obj: object, ctx: AttrContext) -> Value:
return GenericValue(dict, [TypedValue(str), TypedValue(types.ModuleType)])

result, _, _ = _get_attribute_from_mro(obj, ctx, on_class=True)
if (
isinstance(result, KnownValue)
and (
safe_isinstance(result.val, types.MethodType)
or safe_isinstance(result.val, types.BuiltinFunctionType)
and result.val.__self__ is obj
)
and isinstance(ctx.root_value, AnnotatedValue)
):
result = UnboundMethodValue(ctx.attr, ctx.root_composite)
if safe_isinstance(obj, type):
result = set_self(result, TypedValue(obj))
if isinstance(obj, (types.ModuleType, type)):
Expand Down
27 changes: 27 additions & 0 deletions pyanalyze/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from .test_name_check_visitor import TestNameCheckVisitorBase
from .test_node_visitor import assert_passes, only_before
from .value import (
AnnotatedValue,
AnySource,
AnyValue,
UnboundMethodValue,
assert_is_value,
GenericValue,
KnownValue,
Expand Down Expand Up @@ -170,6 +172,31 @@ def test(x: Union[Capybara, Paca]) -> None:
x.attr, MultiValuedValue([TypedValue(int), TypedValue(str)])
)

@assert_passes()
def test_annotated_known(self):
from typing_extensions import Annotated, Literal
from pyanalyze.extensions import LiteralOnly
from pyanalyze.stacked_scopes import Composite, VarnameWithOrigin
from pyanalyze.value import CustomCheckExtension
from qcore.testing import Anything

origin = VarnameWithOrigin("encoding", Anything) # E: incompatible_argument

def capybara():
encoding: Annotated[Literal["ascii"], LiteralOnly()] = "ascii"
assert_is_value(
encoding.encode,
UnboundMethodValue(
"encode",
Composite(
AnnotatedValue(
KnownValue("ascii"), [CustomCheckExtension(LiteralOnly())]
),
origin,
),
),
)

@assert_passes()
def test_optional_operation(self):
from typing import Optional
Expand Down