Skip to content

Commit 0a1ebee

Browse files
stroxlerfacebook-github-bot
authored andcommitted
Support {typing,typing_extensions}.override
Summary: THe `typing_extensions.override` was added a few months ago, we should support it. The `typing.override` decorator isn't landed yet, but that's okay - we should support it in override checks; it will still raise a "no such name" method when Pyre is being run against a typeshed that doens't have it yet. The PR to add `typing.override` to CPython is out for review now: https://github.com/python/cpython/pull/101564` Reviewed By: grievejia Differential Revision: D43055550 fbshipit-source-id: 3f51f643063afea75bf54ef38ae3a48681b99bd9
1 parent 6e8dd15 commit 0a1ebee

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

source/analysis/test/integration/methodTest.ml

+26-8
Original file line numberDiff line numberDiff line change
@@ -2610,7 +2610,7 @@ let test_check_override_decorator context =
26102610
let assert_type_errors = assert_type_errors ~context in
26112611
assert_type_errors
26122612
{|
2613-
from pyre_extensions import override
2613+
from typing import override
26142614

26152615
class Foo:
26162616
def different_method(self, input: int) -> int:
@@ -2627,7 +2627,7 @@ let test_check_override_decorator context =
26272627
];
26282628
assert_type_errors
26292629
{|
2630-
from pyre_extensions import override
2630+
from typing import override
26312631

26322632
class Foo:
26332633
@staticmethod
@@ -2645,7 +2645,7 @@ let test_check_override_decorator context =
26452645
];
26462646
assert_type_errors
26472647
{|
2648-
from pyre_extensions import override
2648+
from typing import override
26492649

26502650
class Foo:
26512651
@classmethod
@@ -2660,7 +2660,7 @@ let test_check_override_decorator context =
26602660
[];
26612661
assert_type_errors
26622662
{|
2663-
from pyre_extensions import override
2663+
from typing import override
26642664

26652665
@override
26662666
def foo(input: int) -> int:
@@ -2672,7 +2672,7 @@ let test_check_override_decorator context =
26722672
];
26732673
assert_type_errors
26742674
{|
2675-
from pyre_extensions import override
2675+
from typing import override
26762676

26772677
def foo_outer(input: int) -> int:
26782678
@override
@@ -2688,7 +2688,7 @@ let test_check_override_decorator context =
26882688
];
26892689
assert_type_errors
26902690
{|
2691-
from pyre_extensions import override
2691+
from typing import override
26922692

26932693
class Foo1:
26942694
def foo(self, input: int) -> int:
@@ -2704,7 +2704,7 @@ let test_check_override_decorator context =
27042704
[];
27052705
assert_type_errors
27062706
{|
2707-
from pyre_extensions import override
2707+
from typing import override
27082708

27092709
class Foo:
27102710
def foo(self, input: int) -> str:
@@ -2719,7 +2719,7 @@ let test_check_override_decorator context =
27192719
needs to be checked. *) ];
27202720
assert_type_errors
27212721
{|
2722-
from pyre_extensions import override
2722+
from typing import override
27232723

27242724
class Foo:
27252725
foo: int = 3
@@ -2729,6 +2729,24 @@ let test_check_override_decorator context =
27292729
foo: str = "hello world"
27302730
|}
27312731
["Parsing failure [404]: invalid syntax"];
2732+
assert_type_errors
2733+
{|
2734+
import typing_extensions
2735+
import pyre_extensions
2736+
2737+
class Bar:
2738+
@pyre_extensions.override
2739+
def method0(self) -> None: ...
2740+
2741+
@pyre_extensions.override
2742+
def method1(self) -> None: ...
2743+
|}
2744+
[
2745+
"Invalid override [40]: `test.Bar.method0` is decorated with @override, but no method of the \
2746+
same name exists in superclasses of `Bar`.";
2747+
"Invalid override [40]: `test.Bar.method1` is decorated with @override, but no method of the \
2748+
same name exists in superclasses of `Bar`.";
2749+
];
27322750
()
27332751

27342752

source/ast/statement.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,11 @@ end = struct
665665

666666
let is_final_method signature = has_decorator signature "typing.final"
667667

668-
let is_override_method signature = has_decorator signature "pyre_extensions.override"
668+
let is_override_method signature =
669+
has_decorator signature "typing.override"
670+
|| has_decorator signature "typing_extensions.override"
671+
|| has_decorator signature "pyre_extensions.override"
672+
669673

670674
let is_dunder_method signature =
671675
let name = unqualified_name signature in

source/test/test.ml

+8-1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ let typeshed_stubs ?(include_helper_builtins = true) () =
10801080
Literal: _SpecialForm = ...
10811081
# TypedDict is a (non-subscriptable) special form.
10821082
TypedDict: object
1083+
def override(f: _F) -> _F: ...
10831084
10841085
Callable: _SpecialForm = ...
10851086
Protocol: _SpecialForm = ...
@@ -1482,7 +1483,13 @@ let typeshed_stubs ?(include_helper_builtins = true) () =
14821483
|};
14831484
( "typing_extensions.pyi",
14841485
{|
1485-
from typing import Final as Final, ParamSpec as ParamSpec, _SpecialForm, overload as overload
1486+
from typing import (
1487+
_SpecialForm,
1488+
Final as Final,
1489+
ParamSpec as ParamSpec,
1490+
overload as overload,
1491+
override as override
1492+
)
14861493
Literal: _SpecialForm = ...
14871494
LiteralString: _SpecialForm = ...
14881495

0 commit comments

Comments
 (0)