-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type[C] #1569
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
Conversation
# Translate constructor fn back to class. | ||
if isinstance(type, CallableType) and type.is_type_obj(): | ||
types[i] = type = type.ret_type | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is right, though somehow it never made a difference in my tests. The value C
(the class) is not the same sort of thing as an instance of the class C
.
Do you plan to add support for invoking a class method on a value of type |
Of course; I hope that already works. --Guido (mobile)
|
No it doesn't. Another reason not to release this yet. :-(
|
Mind having another look? I did add support for class methods, at
least for a few cases.
|
The TypeC tests no pass except for one ProUser* that should be ProUser; but something's seriously wrong because many other tests now fail.
- Add tests for Type[Any] and plain Type, and fix a bug that found. - Respond to easy review feedback (adding comments/docstrings mostly).
We only support two special cases (hopefully the most common ones): - Type[<concrete_class>] - Type[<type_variable_with_concrete_class_upper_bound>]
(This happens when type checking a call, not during semantic analysis.)
Another look? I've addressed all XXX comments, and added more tests. Would like to get this in 0.4.2. |
Ignore AppVeyor. I don't know why it keeps doing this even though I removed appveyor.yml. |
class User: pass | ||
class ProUser(User): pass | ||
def new_user(user_class: Type[User]) -> User: | ||
return user_class() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test calling with invalid arguments to __init__
. Also test a class with non-default __init__
and call constructor with both valid and invalid arguments.
Please ignore the AppVeyor build, as usual. |
Addressed all remaining comments. |
elif isinstance(self.s, Instance) and self.s.type.fullname() == 'builtins.type': | ||
return self.s | ||
else: | ||
return AnyType() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't return AnyType
here -- instead, use self.default(self.s)
, which usually returns object
.
Looks pretty good now! Here are a few additional things to test:
These are optional but if you don't implement these consider creating issues:
|
Pushed solutions for the first 4 bullets. |
Looks like Type[C] is indeed a subtype of Callable[[], C]: class C: pass
T = Type[C]
def f(a: Callable[[], C]) -> None: pass
f(C)
f(T) has no errors. I can write a more elaborate test after lunch. UPDATE: But I'm not sure what the last two bullets imply then. |
TBD