-
Notifications
You must be signed in to change notification settings - Fork 237
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
Alternative constructors with primitive types or NewTypes #415
Comments
I don't think More broadly, we've generally avoided putting things in |
I agree that it's not common, in part because it's not possible in the
current ecosystem. I also agree that it doesn't necessarily need to be
added to typing, but I'd like this to at least be possible.
In addition, the runtime overhead will be directly caused by the person who
defined the type constructor, not the typing library. This is essentially
what happens in a class constructor.
…On Tue, Apr 25, 2017 at 6:12 PM Jelle Zijlstra ***@***.***> wrote:
I don't think cast is similar to NewType; you can't use a cast in a type
annotation.
More broadly, we've generally avoided putting things in typing that
affect runtime behavior. I'm not sure what you're asking for is common
enough to be added to typing.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#415 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AF3tuXK22ik9UDZx2yd2x8az1QghxNAcks5rzm_mgaJpZM4NH6WT>
.
|
Re: cast vs NewType: Fair enough, NewType allows for type annotions, which is very valuable, and which is also why I believe that some variant would be useful. The casting of the type is essentially what happens when the inner/hidden new_type function (https://github.com/python/typing/blob/master/src/typing.py#L2178) is called, and that to me is the value in using NewType's constructor (i.e. calling the inner new_type function). To me, it seems as though this inner new_type function is an optimized special case of something more general. I may be wrong, but customization of this function will not likely add too much overhead to someone who opts out of such a customization and just uses the supported defaults. |
I mostly agree, I think this is more suitable for runtime type enforcement libs like enforce, typecheck-decorator, or typeguard. There is still something that could be supported statically on literal level, like positive/negative numbers, fixed length/value strings, but we already have an issue for this in mypy python/mypy#3062. |
I'd probably have a custom constructor for NewType values. For example: MyType = NewType('MyType', str)
def make_my_type(x: str) -> MyType:
# do whatever validation and initialization that is needed
return <whatever> You can still call |
I've tried most work-arounds and in my case I've settled with a validator class that maintains internal properties for validation. Unfortunately it looks like the new method doing fancy stuff is of low priority for now python/mypy#1020. I do like the idea of adding a lint test, though for some cases EDIT: Realizing that my references to NamedTuple were app-specific modifications |
NewType had many limitations: not supported in sphinx, not possible to override __repr__, and requirement for standalone helper constructors, like: python/typing#415 (comment) My reservations about creating a custom class was performance, but I hope that was addressed by inheriting from float (or tuple, in the case of coords) and setting empty __slots__.
Static type checking can't execute Python code, as by now we have type checkers written in Python, C, JavaScript, Java. Therefore I am closing this here. |
Hi typing team,
I've noticed that typing.cast and NewType essentially provide the exact same functionality. This makes NewType merely a type alias for convenience.
I'd like NewType to provide the ability to either pass in a callable that serves as the constructor, or for the library to provide an alternative function called ValidatedType (or AlternativelyConstructedType or something like that), implemented in this way (or something similar):
This may add some overhead, but it also adds more clarity to codebases and is worth considering.
See python/mypy#3200 -- created this issue here since mypy might not be the right repo to direct this feature request to
The text was updated successfully, but these errors were encountered: