Skip to content
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

Add a dummy object to designate "uninitialized" networks (fixes #511) #525

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

acolomb
Copy link
Collaborator

@acolomb acolomb commented Aug 7, 2024

This class can replace the dummy "None" value for attribute initializations, which can then be properly typed as Network to avoid static type checking errors.

Switch to that new initialization in the NmtBase class as an example.

This has the benefit of not needing self.network is not None checks at run-time wherever a method or attribute access is used, but still satisfies static type checking. When hitting such a code path at run-time, of course it will lead to an exception because the attributes required in the Network methods are not set. But that is a case of wrong API usage (accessing a network without associating it first), which a static checker cannot detect reliably. The dummy class could be modified to provide a better exception message if desired, but this is just a minimal proof of concept so far.

This is an alternative approach to #513. If the concept looks acceptable, it will be extended to other places with the same type checker errors.

This class can replace the dummy "None" value for attribute
initializations, which can then be properly typed as Network to avoid
static type checking errors.

Switch to that new initialization in the NmtBase class as an example.

This has the benefit of not needing `self.network is not None` checks
at run-time wherever a method or attribute access is used, but still
satisfies static type checking.  When hitting such a code path at
run-time, of course it will lead to an exception because the
attributes required in the Network methods are not set.  But that is a
case of wrong API usage (accessing a network without associating it
first), which a static checker cannot detect reliably.  The dummy
class could be modified to provide a better exception message if
desired, but this is just a minimal proof of concept so far.
This works because `from foo import bar` requires full module
initialization at import time, while simply importing the module name
does not.
@acolomb
Copy link
Collaborator Author

acolomb commented Aug 7, 2024

@erlend-aasland What do you think of this approach, before I start fleshing it out in more detail?

@erlend-aasland
Copy link
Contributor

@erlend-aasland What do you think of this approach, before I start fleshing it out in more detail?

I'm not totally convinced, but I like it better than sprinkling the code with conditionals.

@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 80.00000% with 9 lines in your changes missing coverage. Please review.

Please upload report for BASE (master@83594ac). Learn more about missing BASE report.

Files Patch % Lines
canopen/node/local.py 40.00% 6 Missing ⚠️
canopen/nmt.py 60.00% 1 Missing and 1 partial ⚠️
canopen/network.py 83.33% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #525   +/-   ##
=========================================
  Coverage          ?   71.20%           
=========================================
  Files             ?       26           
  Lines             ?     3129           
  Branches          ?      527           
=========================================
  Hits              ?     2228           
  Misses            ?      770           
  Partials          ?      131           
Files Coverage Δ
canopen/emcy.py 97.22% <100.00%> (ø)
canopen/lss.py 41.75% <100.00%> (ø)
canopen/node/base.py 100.00% <100.00%> (ø)
canopen/node/remote.py 67.46% <100.00%> (ø)
canopen/pdo/base.py 65.24% <100.00%> (ø)
canopen/sdo/base.py 77.65% <100.00%> (ø)
canopen/network.py 91.12% <83.33%> (ø)
canopen/nmt.py 91.91% <60.00%> (ø)
canopen/node/local.py 85.54% <40.00%> (ø)

Use a _DummyNetwork object instead.  Fix up check for "network is
None" by providing a convenience has_network() function in the base
class.
@acolomb acolomb marked this pull request as ready for review August 11, 2024 21:34
Use a _DummyNetwork object instead.
@@ -279,6 +279,17 @@ def __len__(self) -> int:
return len(self.nodes)


class _DummyNetwork(Network):
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure about the naming. I'd prefer if the naming more closely reflected it's purpose:

  • _NetworkPlaceholder
  • _UninitializedNetwork
  • _EmptyNetwork

canopen/node/local.py Outdated Show resolved Hide resolved
Copy link
Contributor

@erlend-aasland erlend-aasland left a comment

Choose a reason for hiding this comment

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

I like this!

canopen/network.py Outdated Show resolved Hide resolved
@erlend-aasland
Copy link
Contributor

(You need to amend the PR title to reflect the name changes.)

@acolomb acolomb changed the title Add a _DummyNetwork class to designate "uninitialized" networks Add a dummy object to designate "uninitialized" networks Aug 12, 2024
@acolomb
Copy link
Collaborator Author

acolomb commented Aug 12, 2024

@sveinse Are you satisfied with this solution instead of #513?

@acolomb acolomb changed the title Add a dummy object to designate "uninitialized" networks Add a dummy object to designate "uninitialized" networks (fixes #511) Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Network instance is optional in many classes, which requires lot of checking
3 participants