Skip to content

Commit

Permalink
Support {typing,typing_extensions}.override
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stroxler authored and facebook-github-bot committed Feb 9, 2023
1 parent 6e8dd15 commit 0a1ebee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
34 changes: 26 additions & 8 deletions source/analysis/test/integration/methodTest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2610,7 +2610,7 @@ let test_check_override_decorator context =
let assert_type_errors = assert_type_errors ~context in
assert_type_errors
{|
from pyre_extensions import override
from typing import override

class Foo:
def different_method(self, input: int) -> int:
Expand All @@ -2627,7 +2627,7 @@ let test_check_override_decorator context =
];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

class Foo:
@staticmethod
Expand All @@ -2645,7 +2645,7 @@ let test_check_override_decorator context =
];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

class Foo:
@classmethod
Expand All @@ -2660,7 +2660,7 @@ let test_check_override_decorator context =
[];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

@override
def foo(input: int) -> int:
Expand All @@ -2672,7 +2672,7 @@ let test_check_override_decorator context =
];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

def foo_outer(input: int) -> int:
@override
Expand All @@ -2688,7 +2688,7 @@ let test_check_override_decorator context =
];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

class Foo1:
def foo(self, input: int) -> int:
Expand All @@ -2704,7 +2704,7 @@ let test_check_override_decorator context =
[];
assert_type_errors
{|
from pyre_extensions import override
from typing import override

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

class Foo:
foo: int = 3
Expand All @@ -2729,6 +2729,24 @@ let test_check_override_decorator context =
foo: str = "hello world"
|}
["Parsing failure [404]: invalid syntax"];
assert_type_errors
{|
import typing_extensions
import pyre_extensions

class Bar:
@pyre_extensions.override
def method0(self) -> None: ...

@pyre_extensions.override
def method1(self) -> None: ...
|}
[
"Invalid override [40]: `test.Bar.method0` is decorated with @override, but no method of the \
same name exists in superclasses of `Bar`.";
"Invalid override [40]: `test.Bar.method1` is decorated with @override, but no method of the \
same name exists in superclasses of `Bar`.";
];
()


Expand Down
6 changes: 5 additions & 1 deletion source/ast/statement.ml
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,11 @@ 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_override_method signature =
has_decorator signature "typing.override"
|| has_decorator signature "typing_extensions.override"
|| has_decorator signature "pyre_extensions.override"


let is_dunder_method signature =
let name = unqualified_name signature in
Expand Down
9 changes: 8 additions & 1 deletion source/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,7 @@ let typeshed_stubs ?(include_helper_builtins = true) () =
Literal: _SpecialForm = ...
# TypedDict is a (non-subscriptable) special form.
TypedDict: object
def override(f: _F) -> _F: ...
Callable: _SpecialForm = ...
Protocol: _SpecialForm = ...
Expand Down Expand Up @@ -1482,7 +1483,13 @@ let typeshed_stubs ?(include_helper_builtins = true) () =
|};
( "typing_extensions.pyi",
{|
from typing import Final as Final, ParamSpec as ParamSpec, _SpecialForm, overload as overload
from typing import (
_SpecialForm,
Final as Final,
ParamSpec as ParamSpec,
overload as overload,
override as override
)
Literal: _SpecialForm = ...
LiteralString: _SpecialForm = ...
Expand Down

0 comments on commit 0a1ebee

Please sign in to comment.