Skip to content

Commit

Permalink
Fix importing TypeAlias in stubgen (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
yosh-matsuda authored Sep 18, 2024
1 parent 5b36d73 commit 7f7e0c0
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,10 @@ def put_type(self, tp: NbType, name: Optional[str]):

if same_module:
# This is an alias of a type in the same module or same top-level module
alias_tp = self.import_object("typing", "TypeAlias")
if sys.version_info >= (3, 10, 0):
alias_tp = self.import_object("typing", "TypeAlias")
else:
alias_tp = self.import_object("typing_extensions", "TypeAlias")
self.write_ln(f"{name}: {alias_tp} = {tp.__qualname__}\n")
elif self.include_external_imports or (same_toplevel_module and self.include_internal_imports):
# Import from a different module
Expand Down Expand Up @@ -581,7 +584,10 @@ def put_value(self, value: object, name: str, parent: Optional[object] = None, a
if self.is_type_var(tp):
types = ""
elif typing.get_origin(value):
types = ": " + self.import_object("typing", "TypeAlias")
if sys.version_info >= (3, 10, 0):
types = ": " + self.import_object("typing", "TypeAlias")
else:
types = ": " + self.import_object("typing_extensions", "TypeAlias")
else:
types = f": {self.type_str(tp)}"

Expand Down

0 comments on commit 7f7e0c0

Please sign in to comment.