-
Notifications
You must be signed in to change notification settings - Fork 216
Closed
astral-sh/ruff
#21157Labels
typing semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc
Milestone
Description
NewType is an almost-zero-runtime-overhead way to create a new subtype:
I64 = NewType('I64', int)
reveal_type(I64) # Currently: NewType (correct, but lacks information)
takes_i64(42) # error
takes_i64(I64(42)) # fine
def _(a: I64):
reveal_type(a) # I64
takes_int(a) # fineOne important thing to note is that I64's type cannot be inferred as type[I64], because at runtime it isn't:
print(I64.__class__) # 3.10 and later: NewType
# 3.9 and older: function
isinstance(42, I64) # errorSpecification: Type aliases § NewType.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
typing semanticstyping-module features, spec compliance, etctyping-module features, spec compliance, etc