8000 Add support for Self type by ilevkivskyi · Pull Request #14041 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Add support for Self type #14041

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b205241
Start working on Self type
ilevkivskyi Nov 7, 2022
5e5bd6f
Add support for special forms; some fixes
ilevkivskyi Nov 7, 2022
7f91f3f
More fixes; more tests
ilevkivskyi Nov 7, 2022
80a11f6
Improve a test
ilevkivskyi Nov 8, 2022
2eb0db1
More tests few more fixes
ilevkivskyi Nov 8, 2022
535d936
Add incremental tests
ilevkivskyi Nov 8, 2022
ca7c7e8
Add docs
ilevkivskyi Nov 8, 2022
2ee66ec
Minor cleanup
ilevkivskyi Nov 8, 2022
ccb74a7
Fix self-compilation
ilevkivskyi Nov 8, 2022
504fe2c
Best effort support for unusual locations for self
ilevkivskyi Nov 8, 2022
1a99961
Some cleanups
ilevkivskyi Nov 9, 2022
ce8d345
Enable ClassVar (to some safe extent)
ilevkivskyi Nov 9, 2022
324eff2
Allow redundant Self by default; add error code
ilevkivskyi Nov 9, 2022
d96cfdc
Prohibit Self with arguments
ilevkivskyi Nov 9, 2022
0b953cf
Address CR; minor cleanups
ilevkivskyi Nov 10, 2022
5829804
Prohibit unclear cases; some more tests
ilevkivskyi Nov 10, 2022
3ec47b9
Make ClassVar in generics better
ilevkivskyi Nov 10, 2022
24dd649
More cleanup
ilevkivskyi Nov 10, 2022
ac6234d
Fix TypeVar id clash
ilevkivskyi Nov 11, 2022
61c0589
Final tweaks + couple tests
ilevkivskyi Nov 12, 2022
cbd97b1
Fix another bug from mypy_primer
ilevkivskyi Nov 12, 2022
362d84a
Fix upper bound for Self
ilevkivskyi Nov 12, 2022
a5740eb
More CR (docstring)
ilevkivskyi Nov 12, 2022
6694f3b
Fix Self import; fix method bodies; simplify id handling
ilevkivskyi Nov 12, 2022
3f86bf2
Allow using plain class in final classes
ilevkivskyi Nov 12, 2022
16e017d
Merge branch 'master' into self-type
ilevkivskyi Nov 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix upper bound for Self
  • Loading branch information
ilevkivskyi committed Nov 12, 2022
commit 362d84a84ac63a11907e91ff960aabb2bcde71e9
3 changes: 2 additions & 1 deletion docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,10 @@ classes are generic, self-type allows giving them precise signatures:
.. code-block:: python

T = TypeVar('T')
Q = TypeVar('Q', bound='Base[Any]')

class Base(Generic[T]):
Q = TypeVar('Q', bound='Base[T]')

def __init__(self, item: T) -> None:
self.item = item

Expand Down
4 changes: 2 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
invalid_recursive_alias,
is_named_instance,
)
from mypy.typevars import fill_typevars, fill_typevars_with_any
from mypy.typevars import fill_typevars
from mypy.util import (
correct_relative_import,
is_dunder,
Expand Down Expand Up @@ -1041,7 +1041,7 @@ def setup_self_type(self) -> None:
f"{info.fullname}.Self",
self.tvar_scope.new_unique_func_id(),
[],
fill_typevars_with_any(info),
fill_typevars(info),
)

def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
Expand Down
12 changes: 11 additions & 1 deletion test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ class C(Generic[T]):
def __init__(self, val: T) -> None: ...
@classmethod
def pair(cls, val: T) -> Tuple[Self, Self]:
return (cls(val), C(val)) # E: Incompatible return value type (got "Tuple[Self, C[Any]]", expected "Tuple[Self, Self]")
return (cls(val), C(val)) # E: Incompatible return value type (got "Tuple[Self, C[T]]", expected "Tuple[Self, Self]")

class D(C[int]): pass
reveal_type(C.pair(42)) # N: Revealed type is "Tuple[__main__.C[builtins.int], __main__.C[builtins.int]]"
Expand Down Expand Up @@ -1600,3 +1600,13 @@ class C(Generic[T]):
def test(x: C[T]) -> T:
reveal_type(x.val) # N: Revealed type is "T`-1"
return x.val

[case testTypingSelfGenericBound]
from typing import Self, Generic, TypeVar

T = TypeVar("T")
class C(Generic[T]):
val: T
def foo(self) -> Self:
reveal_type(self.val) # N: Revealed type is "T`1"
return self
0