PEP 698: Add "self" parameters to methods and a colon to a class (#3490)
This commit is contained in:
parent
fb526a8822
commit
ad131c6132
|
@ -196,7 +196,7 @@ method or attribute in some ancestor class.
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def baz() -> int: # Type check error: no matching signature in ancestor
|
def baz(self) -> int: # Type check error: no matching signature in ancestor
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
@ -240,7 +240,7 @@ Consider the following code:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Child(Parent):
|
class Child(Parent):
|
||||||
def foo() -> int:
|
def foo(self) -> int:
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
Imagine we refactor it as follows:
|
Imagine we refactor it as follows:
|
||||||
|
@ -248,12 +248,12 @@ Imagine we refactor it as follows:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
class Parent
|
class Parent:
|
||||||
def foo() -> int: # This method is new
|
def foo(self) -> int: # This method is new
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
class Child(Parent):
|
class Child(Parent):
|
||||||
def foo() -> int: # This is now an override!
|
def foo(self) -> int: # This is now an override!
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
def call_foo(parent: Parent) -> int:
|
def call_foo(parent: Parent) -> int:
|
||||||
|
@ -416,22 +416,22 @@ ancestor class where the overridden method should be defined:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
class Parent0:
|
class Parent0:
|
||||||
def foo() -> int:
|
def foo(self) -> int:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class Parent1:
|
class Parent1:
|
||||||
def bar() -> int:
|
def bar(self) -> int:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class Child(Parent0, Parent1):
|
class Child(Parent0, Parent1):
|
||||||
@override(Parent0) # okay, Parent0 defines foo
|
@override(Parent0) # okay, Parent0 defines foo
|
||||||
def foo() -> int:
|
def foo(self) -> int:
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
@override(Parent0) # type error, Parent0 does not define bar
|
@override(Parent0) # type error, Parent0 does not define bar
|
||||||
def bar() -> int:
|
def bar(self) -> int:
|
||||||
return 2
|
return 2
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue