From 6e8dd15dce4dfa5698477c74aca69a8a8d1c88a0 Mon Sep 17 00:00:00 2001 From: Steven Troxler Date: Thu, 9 Feb 2023 13:20:17 -0800 Subject: [PATCH] Extract is_override_method check Summary: Instead of inlining the name of the override decorator we support directly into typeCheck.ml, let's encode it in a helper function of the Statement.Define module, consistent with how we handle other special decorators like `final`. Reviewed By: kinto0 Differential Revision: D43055549 fbshipit-source-id: ca34452c739196f4e4f9feede216b88da8cb3ed5 --- source/analysis/typeCheck.ml | 4 +--- source/ast/statement.ml | 8 ++++++++ source/ast/statement.mli | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/source/analysis/typeCheck.ml b/source/analysis/typeCheck.ml index 4286b3c7fe0..dcebbf4f63c 100644 --- a/source/analysis/typeCheck.ml +++ b/source/analysis/typeCheck.ml @@ -5347,9 +5347,7 @@ module State (Context : Context) = struct errors in let check_override_decorator errors = - let override_decorator_name = "pyre_extensions.override" in - let has_override_decorator = StatementDefine.has_decorator define override_decorator_name in - if has_override_decorator then + if StatementDefine.is_override_method define then match define with | { Ast.Statement.Define.signature = { parent = Some parent; _ }; _ } -> ( let possibly_overridden_attribute = diff --git a/source/ast/statement.ml b/source/ast/statement.ml index 70a2c04c287..2c6dc972c88 100644 --- a/source/ast/statement.ml +++ b/source/ast/statement.ml @@ -408,6 +408,8 @@ and Define : sig val is_final_method : t -> bool + val is_override_method : t -> bool + val is_class_method : t -> bool val is_class_property : t -> bool @@ -496,6 +498,8 @@ and Define : sig val is_final_method : t -> bool + val is_override_method : t -> bool + val is_class_method : t -> bool val is_class_property : t -> bool @@ -661,6 +665,8 @@ end = struct let is_final_method signature = has_decorator signature "typing.final" + let is_override_method signature = has_decorator signature "pyre_extensions.override" + let is_dunder_method signature = let name = unqualified_name signature in String.is_prefix ~prefix:"__" name && String.is_suffix ~suffix:"__" name @@ -859,6 +865,8 @@ end = struct let is_final_method { signature; _ } = Signature.is_final_method signature + let is_override_method { signature; _ } = Signature.is_override_method signature + let is_dunder_method { signature; _ } = Signature.is_dunder_method signature let is_class_method { signature; _ } = Signature.is_class_method signature diff --git a/source/ast/statement.mli b/source/ast/statement.mli index 14af4a37f90..26cd2bd0c62 100644 --- a/source/ast/statement.mli +++ b/source/ast/statement.mli @@ -161,6 +161,8 @@ and Define : sig val is_final_method : t -> bool + val is_override_method : t -> bool + val is_class_method : t -> bool val is_class_property : t -> bool @@ -243,6 +245,8 @@ and Define : sig val is_final_method : t -> bool + val is_override_method : t -> bool + val is_class_method : t -> bool val is_class_property : t -> bool