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

ORM: Fix deprecation warning being shown for new code types #6407

Merged
merged 1 commit into from
May 21, 2024

Conversation

sphuber
Copy link
Contributor

@sphuber sphuber commented May 21, 2024

The InstalledCode and PortableCode have been introduced already some time ago as the refactored version of the legacy Code class. The latter has been deprecated for that reason, but for backwards compatibility reasons, the new implementations still need to keep the legacy class as a base class. Unfortunately, this meant that the deprecation warning was also shown when users instantiated an instance of the new classes, which would be confusing.

The deprecation warning had already been moved from module level to the constructor of the Code class to prevent it from showing merely upon import of the aiida.orm package. As a final work around, the new classes define the _EMIT_CODE_DEPRECATION_WARNING class attribute which is checked in the Code constructor such that when set, the deprecation warning is skipped.

@sphuber sphuber requested a review from danielhollas May 21, 2024 19:55
@sphuber
Copy link
Contributor Author

sphuber commented May 21, 2024

This seems to do the trick. Credits to @giovannipizzi I remember him mentioning an approach like this somewhere sometime ago.

Copy link
Collaborator

@danielhollas danielhollas left a comment

Choose a reason for hiding this comment

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

LGTM! Just one ask for making a comment about how this workaround works.

I tested locally that verdi code create core.code.installed does not emit warning, while verdi code create core.code does.

'`isinstance`, use `aiida.orm.nodes.data.code.abstract.AbstractCode`.',
version=3,
)
if getattr(self, '_EMIT_CODE_DEPRECATION_WARNING', True):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just for my own understanding; why do we need to use getattr instead accessing via self directly?

Ah, took me a while to understand, this is evil. :D The trick is that this attribute is only defined in child classes, right? Maybe a comment explaining this trick is warranted here.

)
if getattr(self, '_EMIT_CODE_DEPRECATION_WARNING', True):
warn_deprecation(
'The `Code` class is deprecated. To create an instance, use the '
Copy link
Collaborator

Choose a reason for hiding this comment

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

This warning might be a bit confusing if it's printed when user tries to create Code via CLI. At the same time the text is already long. 🤷

The `InstalledCode` and `PortableCode` have been introduced already some
time ago as the refactored version of the legacy `Code` class. The
latter has been deprecated for that reason, but for backwards
compatibility reasons, the new implementations still need to keep the
legacy class as a base class. Unfortunately, this meant that the
deprecation warning was also shown when users instantiated an instance
of the new classes, which would be confusing.

The deprecation warning had already been moved from module level to the
constructor of the `Code` class to prevent it from showing merely upon
import of the `aiida.orm` package. As a final work around, the new
classes define the `_EMIT_CODE_DEPRECATION_WARNING` class attribute
which is checked in the `Code` constructor such that when set, the
deprecation warning is skipped.
@sphuber sphuber force-pushed the fix/code-deprecation-warning branch from 2ced904 to aaaba67 Compare May 21, 2024 21:03
@sphuber sphuber merged commit a915571 into aiidateam:main May 21, 2024
19 checks passed
@sphuber sphuber deleted the fix/code-deprecation-warning branch May 21, 2024 21:35
mikibonacci pushed a commit to mikibonacci/aiida-core that referenced this pull request Sep 3, 2024
…m#6407)

The `InstalledCode` and `PortableCode` have been introduced already some
time ago as the refactored version of the legacy `Code` class. The
latter has been deprecated for that reason, but for backwards
compatibility reasons, the new implementations still need to keep the
legacy class as a base class. Unfortunately, this meant that the
deprecation warning was also shown when users instantiated an instance
of the new classes, which would be confusing.

The deprecation warning had already been moved from module level to the
constructor of the `Code` class to prevent it from showing merely upon
import of the `aiida.orm` package. As a final work around, the new
classes define the `_EMIT_CODE_DEPRECATION_WARNING` class attribute
which is checked in the `Code` constructor such that when set, the
deprecation warning is skipped.
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.

2 participants