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

Further simplify annotations.py #630

Merged
merged 2 commits into from
May 24, 2023
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

- Remove more old special cases and improve robustness of
annotation parsing (#630)
- Remove dependency on `typing_inspect` (#629)
- Fix use of `Literal` types with `typing_extensions` 4.6.0 (#628)

Expand Down
31 changes: 7 additions & 24 deletions pyanalyze/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
ContextManager,
Generator,
Mapping,
NamedTuple,
NewType,
Optional,
Sequence,
Expand Down Expand Up @@ -412,6 +411,8 @@ def _type_from_runtime(
return _value_of_origin_args(
origin, args, val, ctx, allow_unpack=allow_unpack, is_typeddict=is_typeddict
)
# Can't use is_typeddict() here because we still want to support
# mypy_extensions.TypedDict
elif is_instance_of_typing_name(val, "_TypedDictMeta"):
required_keys = getattr(val, "__required_keys__", None)
# 3.8's typing.TypedDict doesn't have __required_keys__. With
Expand All @@ -438,16 +439,9 @@ def _type_from_runtime(
# InitVar instances aren't being created
# static analysis: ignore[undefined_attribute]
return type_from_runtime(val.type)
elif is_instance_of_typing_name(val, "_AnnotatedAlias"):
# Annotated in typing and newer typing_extensions
return _make_annotated(
_type_from_runtime(val.__origin__, ctx),
[KnownValue(v) for v in val.__metadata__],
ctx,
)
elif val is AsynqCallable:
return CallableValue(Signature.make([ELLIPSIS_PARAM], is_asynq=True))
elif val is typing.Any:
elif is_typing_name(val, "Any"):
return AnyValue(AnySource.explicit)
elif isinstance(val, type):
return _maybe_typed_value(val)
Expand Down Expand Up @@ -492,9 +486,6 @@ def _type_from_runtime(
elif val is Ellipsis:
# valid in Callable[..., ]
return AnyValue(AnySource.explicit)
elif is_instance_of_typing_name(val, "_TypeAlias"):
# typing.Pattern and Match, which are not normal generic types for some reason
return GenericValue(val.impl_type, [_type_from_runtime(val.type_var, ctx)])
elif isinstance(val, TypeGuard):
return AnnotatedValue(
TypedValue(bool),
Expand All @@ -517,7 +508,7 @@ def _type_from_runtime(
return AnyValue(AnySource.incomplete_annotation)
elif is_typing_name(val, "TypedDict"):
return KnownValue(TypedDict)
elif val is NamedTuple:
elif is_typing_name(val, "NamedTuple"):
return TypedValue(tuple)
else:
ctx.show_error(f"Invalid type annotation {val}")
Expand Down Expand Up @@ -548,7 +539,7 @@ def _callable_args_from_runtime(
(arg,) = arg_types
if arg is Ellipsis:
return [ELLIPSIS_PARAM]
elif is_typing_name(getattr(arg, "__origin__", None), "Concatenate"):
elif is_typing_name(get_origin(arg), "Concatenate"):
return _args_from_concatenate(arg, ctx)
elif is_instance_of_typing_name(arg, "ParamSpec"):
param_spec = TypeVarValue(arg, is_paramspec=True)
Expand Down Expand Up @@ -576,7 +567,7 @@ def _callable_args_from_runtime(
"__P", kind=ParameterKind.PARAM_SPEC, annotation=param_spec
)
return [param]
elif is_typing_name(getattr(arg_types, "__origin__", None), "Concatenate"):
elif is_typing_name(get_origin(arg_types), "Concatenate"):
return _args_from_concatenate(arg_types, ctx)
else:
ctx.show_error(f"Invalid arguments to {label}: {arg_types!r}")
Expand Down Expand Up @@ -821,11 +812,6 @@ def _maybe_get_extra(origin: type) -> Union[type, str]:
elif any(origin is cls for cls in ASYNC_CONTEXT_MANAGER_TYPES):
return "contextlib.AbstractAsyncContextManager"
else:
# turn typing.List into list in some Python versions
# compare https://github.com/ilevkivskyi/typing_inspect/issues/36
extra_origin = getattr(origin, "__extra__", None)
if extra_origin is not None:
return extra_origin
return origin


Expand Down Expand Up @@ -1095,7 +1081,7 @@ def _value_of_origin_args(
UnionType is not None and origin is UnionType
):
return unite_values(*[_type_from_runtime(arg, ctx) for arg in args])
elif origin is Callable or origin is typing.Callable:
elif origin is Callable or is_typing_name(origin, "Callable"):
if len(args) == 0:
return TypedValue(Callable)
*arg_types, return_type = args
Expand Down Expand Up @@ -1167,9 +1153,6 @@ def _value_of_origin_args(
ctx.show_error("Unpack requires a single argument")
return AnyValue(AnySource.error)
return UnpackedValue(_type_from_runtime(args[0], ctx))
elif origin is None and isinstance(val, type):
# This happens for SupportsInt in 3.7.
return _maybe_typed_value(val)
else:
ctx.show_error(
f"Unrecognized annotation {origin}[{', '.join(map(repr, args))}]"
Expand Down