Skip to content

Commit

Permalink
Reorganize TypeGuard code a bit
Browse files Browse the repository at this point in the history
Summary:
I'm about to add support for `TypeIs` to Pyre, and a majority of the code
should be shared with `TypeGuard`.

There are a few places where the existing code isn't optimized for extending
the concept of type guards to be more general, either due to poor factoring
or names that aren't as clear as they could be. This is a simple commit
with no functional changes to clean some of these up in preparation for
`TypeIs` support.

Reviewed By: grievejia

Differential Revision: D59734070

fbshipit-source-id: c892c2b526fbbc5a1fc17aeb4f74d138c065e316
  • Loading branch information
stroxler authored and facebook-github-bot committed Jul 15, 2024
1 parent 2ebd213 commit 5a3ead8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion source/analysis/type.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4612,7 +4612,7 @@ let expression_contains_any expression =
|> List.exists ~f:(Hashtbl.mem primitives_with_any_map)


let type_inside_typeguard = function
let inner_type_if_is_typeguard = function
| Parametric
{
name = "typing.TypeGuard" | "typing_extensions.TypeGuard";
Expand Down
2 changes: 1 addition & 1 deletion source/analysis/type.mli
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ val is_untyped : t -> bool

val contains_variable : t -> bool

val type_inside_typeguard : t -> t option
val inner_type_if_is_typeguard : t -> t option

val parameters : t -> Parameter.t list option

Expand Down
2 changes: 1 addition & 1 deletion source/analysis/typeCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4157,7 +4157,7 @@ module State (Context : Context) = struct
{ arguments = { Call.Argument.name = None; value = { Node.value = Name name; _ } } :: _; _ }
when is_simple_name name -> (
let { TypeInfo.Unit.annotation = callee_type; _ } = resolve_expression ~resolution test in
match Type.type_inside_typeguard callee_type with
match Type.inner_type_if_is_typeguard callee_type with
| Some guard_type ->
let resolution = refine_local ~name (TypeInfo.Unit.create_mutable guard_type) in
Value resolution
Expand Down

0 comments on commit 5a3ead8

Please sign in to comment.