Skip to content
Prev Previous commit
Next Next commit
Add return type hint
  • Loading branch information
pkess committed Feb 23, 2025
commit e4efeb2ebd008e58a152b4a83d45704666f2eee0
2 changes: 1 addition & 1 deletion can/io/trc.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def __init__(
self.first_timestamp = None
self._setup_file_version(file_version)

def _setup_file_version(self, file_version: Union[int, TRCFileVersion]):
def _setup_file_version(self, file_version: Union[int, TRCFileVersion]) -> None:
try:
self.file_version = TRCFileVersion(file_version)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance attributes should be set in __init__. You could have version and format string as return values of a static method or just remove this method and put the try/except block into __init__

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. The branch where i picked this from handled it with a tuple returned. I think this does not improve readability.

As this method is invoked from the init function it is always ensured that the attributes are initialized. Can you tell my why it should be added explicit in init?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a good practice, and it helps with code completion since IDEs usually look into __init__ for instance attributes.

I wonder why pylint is silent, there's a rule for this: https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/attribute-defined-outside-init.html 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I googled a bit for this and it seems like the only requirement is that all attributes are already initialized after the init function was executed. It is not enforced that the assignment is inside of the init function. So for me it looks like the approach here is correct.

I prefer to try to make a function implementation fit on one or two screen page. With this implementation we are quite prepared for more additions and changes not to blow up the function.

If we return a tuple we would achieve the same that is correct, but i think it is better readable like this.

So please take another look and tell me your thoughts.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts are still the same. Instance attributes belong inside __init__. Here is the output of pyrefly for example:

ERROR /home/user/repositories/python-can/can/io/trc.py:317:13-34: Object of class `TextIO` has no attribute `reconfigure` [missing-attribute]
ERROR /home/user/repositories/python-can/can/io/trc.py:329:18-30: Attribute `file_version` is implicitly defined by assignment in method `_setup_file_version`, which is not a constructor [implicitly-defined-attribute]
ERROR /home/user/repositories/python-can/can/io/trc.py:330:18-33: Attribute `_msg_fmt_string` is implicitly defined by assignment in method `_setup_file_version`, which is not a constructor [implicitly-defined-attribute]

So, it would be nice if you fix it, so i can merge.

self._msg_fmt_string = self.MESSAGE_FORMAT_MAP[self.file_version]
Expand Down