Ad
  • Custom User Avatar

    Yeah, it doesnt really matter if you call rich.inspect(ui) or rich.inspect(UserInfo) - output should be +/- the same.

    You can try to run this example with mypy or other type python type "validators/checkers". They will tell you that you always should typehint all possible values, like str or None.

    This typehint doesn't really do anything, they are just HINTS. Like TypeVars, TypeAliases, etc.

    Cool conversation,. tbh, thank you

  • Custom User Avatar

    As you can see - typehint for _field_no_pipe: str = None was not applied for help docstring

    Thank you for your feedback! I may be over looking what you're trying to explain. On line 533 of your example, str = None is applied to _field_no_pipe in the help doctstring when you use the rich.inspect(, help=True) method on the ui object, no?

    So, are you referring to the print out on line 535 of your example: '_field_no_pipe', <class 'str'>? This is where you feel it should display str | None instead, so future developers will know this field type can be None as well, correct? However, this is the obj type and not the help doctstring of the obj, correct? Again, thank you for feedback!

  • Custom User Avatar

    In doc's example they are using pipes for fields too. When no = field() applied to self.variables in @dataclass - dataclasses appy it automatically. I've wrote this little experiment to test typehinting:

    In [530]: @dataclass
         ...: class UserInfo:
         ...:     _not_field: str | None = None
         ...:     _field_pipe_none: str | None = field(default=None)
         ...:     _field_no_pipe: str = field(default=None)
         ...:
         ...:     def __repr__(self):
         ...:         return f'{self.__class__.__name__}'\
         ...:                f'({self._not_field=}, {self._field_pipe_none=}, {self._field_no_pipe=})'
         ...:
    
    In [531]: ui = UserInfo()
    
    In [532]: ui
    Out[532]: UserInfo(self._not_field=None, self._field_pipe_none=None, self._field_no_pipe=None)
    
    In [533]: rich.inspect(ui, help=True)
    ╭────────────────────────────────────── <class '__main__.UserInfo'> ───────────────────────────────────────╮
    │ UserInfo(_not_field: str | None = None, _field_pipe_none: str | None = None, _field_no_pipe: str = None) │
    │                                                                                                          │
    │ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
    │ │ UserInfo(self._not_field=None, self._field_pipe_none=None, self._field_no_pipe=None)                 │ │
    │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
    │                                                                                                          │
    │ 33 attribute(s) not shown. Run inspect(inspect) for options.                                             │
    ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯
    
    In [534]: from dataclasses import fields
    
    In [535]:  [(field.name, field.type, field.default_factory, field._field_type) for field in fields(ui)]
    Out[535]:
    [
        ('_not_field', str | None, <dataclasses._MISSING_TYPE object at 0x00000251C6FF4040>, _FIELD),
        ('_field_pipe_none', str | None, <dataclasses._MISSING_TYPE object at 0x00000251C6FF4040>, _FIELD),
        ('_field_no_pipe', <class 'str'>, <dataclasses._MISSING_TYPE object at 0x00000251C6FF4040>, _FIELD)
    ]
    

    As you can see - typehint for _field_no_pipe: str = None was not applied for help docstring and future developers will not know that fields job and hobbies can be None (and they can be!), so | None is required.

    P.S. I don't really agree with statement Init-only can have no "| None" in typehints, because it can cause some trouble with later computation. And because of that C# and Kotlin have quistion marks, that indicating that some var can be null (someVar?.name). You should always apply all posible types that variable can be. In this case - job and hobbies can be optional fields for user.

  • Custom User Avatar

    Thanks for the feedback. According to the Python docs, it uses this | None = None typehint only for Init-only variables. Please let me know your feedback if you agree or disagree ~ thanks!

  • Default User Avatar
  • Custom User Avatar

    typehint should also contain | None, even when field(defalut=None) is applied

  • Custom User Avatar

    Can someone explain pls whatwill happen is this scenario:

    User have rank -7, task have rank 4

    diff_progress = current_progress + 10 * (user_rank - task_rank)^2 - because task_rank > user_rank
    diff_progress = 0 + 10 * (-7-4)^2 = 10 * 121 = 1210

    So, user rank changes from -7 to -6 and diff_progress becomes 1110 (-100 for rank up)
    But what will happen with diff_progress after this? I should recalculate it on each step of ranking up? Task description is very unclear...