Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 10 additions & 6 deletions challenges/advanced-type/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@


def make_object(cls):
return cls()
...


## End of your code ##
from typing import assert_type


class MyClass:
pass

Expand All @@ -18,8 +21,9 @@ def f():
pass


c = make_object(MyClass)
c = make_object(int)
c = make_object(f) # expect-type-error
c = make_object("sss") # expect-type-error
c = make_object(["sss"]) # expect-type-error
assert_type(make_object(MyClass), MyClass)
assert_type(make_object(int), int)

make_object(f) # expect-type-error
make_object("sss") # expect-type-error
make_object(["sss"]) # expect-type-error
20 changes: 11 additions & 9 deletions challenges/advanced-type/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
`make_object` takes a class returns an instance of it.
"""

from typing import Any


def make_object(cls: type[Any]):
return cls()
def make_object[T](cls: type[T]) -> T:
...


## End of your code ##
from typing import assert_type


class MyClass:
pass

Expand All @@ -20,8 +21,9 @@ def f():
pass


c = make_object(MyClass)
c = make_object(int)
c = make_object(f) # expect-type-error
c = make_object("sss") # expect-type-error
c = make_object(["sss"]) # expect-type-error
assert_type(make_object(MyClass), MyClass)
assert_type(make_object(int), int)

make_object(f) # expect-type-error
make_object("sss") # expect-type-error
make_object(["sss"]) # expect-type-error
Loading