-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for custom metaclasses that inject instance variables i…
…nto the classes they construct. This addresses #5897. (#5898) Co-authored-by: Eric Traut <[email protected]>
- Loading branch information
1 parent
ffb6eb8
commit 0602883
Showing
5 changed files
with
112 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/pyright-internal/src/tests/samples/metaclass11.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This sample verifies that the type checker allows access | ||
# to instance variables provided by a metaclass. | ||
|
||
from enum import Enum | ||
from typing import Mapping | ||
|
||
|
||
class Meta(type): | ||
var0 = 3 | ||
|
||
def __init__(cls, name, bases, dct): | ||
cls.var1 = "hi" | ||
|
||
|
||
class MyClass(metaclass=Meta): | ||
pass | ||
|
||
|
||
# This should generate an error because var0 isn't | ||
# accessible via an instance of this class. | ||
MyClass().var0 | ||
reveal_type(MyClass.var0, expected_text="int") | ||
MyClass.var0 = 1 | ||
|
||
reveal_type(MyClass().var1, expected_text="str") | ||
reveal_type(MyClass.var1, expected_text="str") | ||
|
||
MyClass.var1 = "hi" | ||
MyClass().var1 = "hi" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters