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

[sonic_platform_base] Proper use of class and instance attributes #173

Merged
merged 2 commits into from
Mar 5, 2021
Merged

[sonic_platform_base] Proper use of class and instance attributes #173

merged 2 commits into from
Mar 5, 2021

Conversation

jleveque
Copy link
Contributor

@jleveque jleveque commented Feb 23, 2021

Description

  • Use class and instance attributes properly. Only use class attributes for data which should be shared among all instances.
  • Import all modules in __init__.py
  • Remove unused imports
  • Clean up whitespace

Motivation and Context

Proper object-oriented programming. This could prevent issues where attributes are shared between instances when they were not meant to be.

This also fixes a bug with PsuBase.psu_master_led_color, where it is defined as both a class attribute and an instance attribute.

With this change, some vendors' APIs can be cleaned up, because they redefined class attributes as instance attributes. That code can be removed.

However, note that vendor APIs MUST call the base class initializer in their initializer methods, or the instance attributes will not be available and will raise exceptions.

How Has This Been Tested?

Tested on one vendor's platform

@lgtm-com

This comment has been minimized.

@lgtm-com

This comment has been minimized.

@lgtm-com

This comment has been minimized.

@jleveque
Copy link
Contributor Author

@Staphylo, @zzhiyuan: FYI. Please make sure Arista concrete platform API classes all call the base (super) class initializer. I went through and took care of a few other platforms in the sonic-buildimage repo:

@jleveque jleveque changed the title [sonic_platform_base] Proper use of class and instance members; other cleanup [sonic_platform_base] Proper use of class and instance attributes Feb 23, 2021
@lgtm-com
Copy link

lgtm-com bot commented Feb 23, 2021

This pull request fixes 1 alert when merging 2110c3a into c6b642b - view on LGTM.com

fixed alerts:

  • 1 for Unused import

self._fan_list = []

# List of ThermalBase-derived objects representing all thermals
# available on the PSU
self._thermal_list = []

self.psu_master_led_color = self.STATUS_LED_COLOR_OFF
PsuBase._psu_master_led_color = self.STATUS_LED_COLOR_OFF
Copy link
Contributor

Choose a reason for hiding this comment

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

@jleveque Thanks for addressing this!

@Staphylo
Copy link
Contributor

@jleveque thanks for the heads up. We'll make the changes.

jleveque pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Feb 24, 2021
 - Fix parent `__init__` call for platform API, based on sonic-net/sonic-platform-common#173
 - Implementation for more platform API methods
 - Daily storage information reporting in `arista daemon`
 - Enhancements to reboot cause reporting, now multi-sourcing reboot cause information
 - Miscellaneous fixes
lguohan pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Feb 25, 2021
…alizer (#6854)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
jleveque added a commit to sonic-net/sonic-buildimage that referenced this pull request Feb 25, 2021
…lizer (#6853)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
jleveque added a commit to sonic-net/sonic-buildimage that referenced this pull request Feb 25, 2021
…ializer (#6852)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
yxieca pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Mar 4, 2021
…ializer (#6852)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
yxieca pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Mar 4, 2021
…lizer (#6853)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
yxieca pushed a commit to sonic-net/sonic-buildimage that referenced this pull request Mar 4, 2021
…alizer (#6854)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
@jleveque jleveque merged commit ed93a15 into sonic-net:master Mar 5, 2021
@jleveque jleveque deleted the fix_class_instance_vars branch March 5, 2021 18:48
carl-nokia pushed a commit to carl-nokia/sonic-buildimage that referenced this pull request Aug 7, 2021
 - Fix parent `__init__` call for platform API, based on sonic-net/sonic-platform-common#173
 - Implementation for more platform API methods
 - Daily storage information reporting in `arista daemon`
 - Enhancements to reboot cause reporting, now multi-sourcing reboot cause information
 - Miscellaneous fixes
carl-nokia pushed a commit to carl-nokia/sonic-buildimage that referenced this pull request Aug 7, 2021
…alizer (sonic-net#6854)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
carl-nokia pushed a commit to carl-nokia/sonic-buildimage that referenced this pull request Aug 7, 2021
…lizer (sonic-net#6853)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
carl-nokia pushed a commit to carl-nokia/sonic-buildimage that referenced this pull request Aug 7, 2021
…ializer (sonic-net#6852)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
lolyu pushed a commit to lolyu/sonic-buildimage that referenced this pull request Sep 13, 2021
…ializer (sonic-net#6852)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
lolyu pushed a commit to lolyu/sonic-buildimage that referenced this pull request Sep 13, 2021
…lizer (sonic-net#6853)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
lolyu pushed a commit to lolyu/sonic-buildimage that referenced this pull request Sep 13, 2021
…alizer (sonic-net#6854)

In preparation for the merging of sonic-net/sonic-platform-common#173, which properly defines class and instance members in the Platform API base classes.

It is proper object-oriented methodology to call the base class initializer, even if it is only the default initializer. This also future-proofs the potential addition of custom initializers in the base classes down the road.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants