8000 Sentinel Object Pattern for <<inherit>> · Issue #3074 · cobbler/cobbler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
SchoolGuy opened this issue Apr 25, 2022 · 2 comments
Open

Sentinel Object Pattern for <<inherit>> #3074

SchoolGuy opened this issue Apr 25, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@SchoolGuy
Copy link
Member

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 a str but just a float. 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)

@SchoolGuy SchoolGuy added the enhancement New feature or request label Apr 25, 2022
@SchoolGuy SchoolGuy moved this to Todo in Cobbler Server Apr 25, 2022
@agraul
Copy link
Contributor
agraul commented Apr 25, 2022

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:

  1. naive approach -> type annotations are completely useless
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"
# pyright 1.py
Found 1 source file
0 errors, 0 warnings, 0 infos 
  1. slightly better approach -> type annotations are as useful as they already are
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"
# pyright 2.py
Found 1 source file
0 errors, 0 warnings, 0 infos 
  1. better approach -> useful type annotation
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"
# pyright 3.py
Found 1 source file
/home/agraul/tmp/cobbler-inherit/3.py
  /home/agraul/tmp/cobbler-inherit/3.py:9:12 - error: Expression of type "Literal['foo']" cannot be assigned to return type "inherit | float"
    Type "Literal['foo']" cannot be assigned to type "inherit | float"
      "Literal['foo']" is incompatible with "inherit"
      "Literal['foo']" is incompatible with "float" (reportGeneralTypeIssues)
1 error, 0 warnings, 0 infos 

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.

@SchoolGuy
Copy link
Member Author

Yes I guess the third approach is the most reasonable in my eyes. But let's see when we have time for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants
0