Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign uprepr of NewType #746
repr of NewType #746
Comments
|
Since NewType is 99.9% meant for the benefit of static type checkers, I'm not sure that I care much about how it is rendered at runtime. If you have a specific issue with this related to Jedi maybe it's best to solve this in Jedi. |
|
In principle, In practice, I think these fiddly ergonomics details can make a huge difference to how easy it is to use an API. For my personal needs a satisfactory solution is to monkey patch import typing
if not TYPE_CHECKING:
class NewType:
def __init__(self, name, tp):
self.__name__ = name
self.__supertype__ = tp
def __call__(self, x):
return x
def __repr__(self):
return self.__name__
typing.NewType = NewType |
|
What about the possibility of updating def formatannotation(annotation, base_module=None):
# Add the following two lines?
# If the annotation is a NewType, return the name of the NewType
if hasattr(annotation, "__qualname__") and annotation.__qualname__ == 'NewType.<locals>.new_type':
return annotation.__name__
if getattr(annotation, '__module__', None) == 'typing':
return repr(annotation).replace('typing.', '') # <== the path taken in NewType case.
if isinstance(annotation, type):
if annotation.__module__ in ('builtins', base_module):
return annotation.__qualname__
return annotation.__module__+'.'+annotation.__qualname__
return repr(annotation) |
|
The more I read about your use case the more I think this should be solved in your tooling, not in Regarding whether it's better to show But as I am not a Jedi user, I'll take your work for it -- I just don't think we should fix this in |
Absolutely a fair point. What about the normal use case for Isn't the point of |
|
It seems the inspect module was not really updated to deal with PEP 484 -- and because PEP 484 does not really care about runtime use of annotations very much. By the time we could fix this we wouldn't need the fix any more, since in 3.10 we'll have I worry that your proposed fix (turning NewType into a class) would slow down common usage -- the docstring promises "almost zero runtime overhead." I fear that an instance with a |
|
It's about 20% slower:
I do agree that it would be useful to get a nicer repr for NewTypes; at my company we use NewTypes heavily in annotations and the current repr() makes it harder to read function signatures in IPython. I wonder if it would be faster if we implemented NewTypeClass in C. |
|
Maybe we could have a class NewTypeClass:
def __init__(self, name, supertype):
self.name = name
self.supertype = supertype
def __call__(self, obj):
return obj
def __repr__(self):
return f"<NewType: {self.name}>"
class IntNewType(NewTypeClass):
__call__ = int
class StrNewType(NewTypeClass):
__call__ = str
def NewType(name, supertype):
if supertype is int:
return IntNewType(name, supertype)
if supertype is str:
return StrNewType(name, supertype)
return NewTypeClass(name, supertype) |
|
you can solve 'is this type a ( the repo isn't ready-for-public, and needs a lot more work on refactoring, renaming, etc, note that I couldn't find a way to get modules from this (relevant issue: #757 ) ps: are you building some kind of 'runtime-type-extraction' for codegens/etc? |
|
@devdoomari3 Thanks for the suggestion, I also made a "is this a |
Currently
NewTypehas a rather unsatisfactoryrepr:outputs:
This (1) doesn't refer to the name of the
NewType, (2) doesn't refer to the wrapped type, (3) doesn't refer to the module theNewTypewas defined in, and (4) is confusing.If a function is defined with the
NewTypein its signature, this repr appears ininspect.getsignature(signature_of_function).For example:
prints
This shows up in Jedi output:
prints
Presumably you could fix this by turning
NewTypeinto a class:This change would also make it less opaque how to test whether an object is a NewType (I think currently the best way to make this check is to say
hasattr(x, "__qualname__") and x.__qualname__ == 'NewType.<locals>.new_type'which is not exactly transparent.I imagine this change could break typecheckers which are likely to care about the implementation details of
NewType, but the fix for the type checkers shouldn't be too complicated.