-
-
Notifications
You must be signed in to change notification settings - Fork 650
Sentinel Object Pattern for <<inherit>> #3074
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
Comments
I guess it will match the sentinel object pattern, but it's a bit more than the naive implementation that's on that page when we want useful type annotations. In this illustrative example, only the third approach covers only use case. There might be an even better way, it's just to illustrate why the naive approach is not right for us:
import typing
inherit = object() # bad for type annotations, match against <class 'object'>
def foo_1() -> typing.Union[object, float]:
return inherit
def foo_2() -> typing.Union[object, float]:
return "foo"
import typing
inherit = "<<inherit>>" # bad for type annotations, match against <class 'str'>
def foo_3() -> typing.Union[str, float]:
return inherit
def foo_4() -> typing.Union[str, float]:
return "foo"
import typing
T_INHERIT = typing.NewType('inherit', str)
inherit = T_INHERIT('<<inherit>>') # good for annotations, match against custom type
def foo5() -> typing.Union[T_INHERIT, float]:
return inherit
def foo6() -> typing.Union[T_INHERIT, float]:
return "foo"
I didn't put any effort into naming the type, for the PR we should come up with something a bit nicer. We'll need to think about the location where to put this as well. |
Yes I guess the third approach is the most reasonable in my eyes. But let's see when we have time for that. |
Is your feature request related to a problem?
Currently many of our method return or argument type definitions are
Union[str, float]
for example. This leads to confusion because in reality they don't allow setting astr
but just afloat
. This is visible in the Docs and thus not very user friendly.Provide a detailed description of the proposed feature
Let's replace all usages where we allow only
<<inherit>>
and another type by the Sentinel Object Pattern.Alternatives you've considered
We could leave it like it is but this leads to confusion and duplication of code. Thus let's not let it stay like it is at the moment if time allows.
Additional information
Originating discussion: #3073 (comment)
The text was updated successfully, but these errors were encountered: