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

index converters by type when no converter is found #1654

Merged
merged 5 commits into from
May 10, 2024

Conversation

braingram
Copy link
Contributor

@braingram braingram commented Sep 28, 2023

This changes how converters with class paths/names as types are handled by asdf.

Prior to PR, when converting a class instance (foo) of class (Foo) the class path of the instance was inspected with get_class_name and the _converters_by_type index was searched using the class name. This index contained both classes and class paths (as converters could use both/either) with classes being preferred. This creates issues when a module has a different class path from the typical 'public' path for the class as the module might move the private implementation without changing the 'public' path with the expectation that this will not break downstream code. For converters that use class paths these types of moves will break the converter.

This PR changes the handling of class paths used for converters so that the 'public' path can be used. If a converter is not found for a class in _converters_by_type (which now only contains classes as keys) then _converters_by_class_path is indexed by checking what classes referred to by the paths are already imported (to avoid importing every class supported by every extension). If the class is imported it is added to _converters_by_type and removed from _converters_by_class_path.

Closes #1653

The changes in this PR will be helpful for maintaining asdf_astropy and other asdf extensions that provide converters for types implemented in a different library. To illustrate this, the following branch was created on a fork of asdf_astropy that uses the public class paths and when run with this PR passes all unit tests:
https://github.com/astropy/asdf-astropy/compare/main...braingram:class_path?expand=1

@braingram braingram added this to the 3.1.0 milestone Sep 28, 2023
@braingram braingram marked this pull request as ready for review October 24, 2023 20:01
@braingram braingram requested a review from a team as a code owner October 24, 2023 20:01
@braingram braingram requested a review from eslavich October 24, 2023 20:01
@braingram braingram force-pushed the class_path branch 2 times, most recently from 3e5cdd6 to 9cef8ef Compare October 26, 2023 16:00
@braingram braingram force-pushed the class_path branch 3 times, most recently from 6d4f55a to af27436 Compare November 22, 2023 21:45
@braingram braingram force-pushed the class_path branch 3 times, most recently from 2e1fd13 to a44def4 Compare February 27, 2024 14:56
@braingram braingram modified the milestones: 3.1.0, 3.2.0 Feb 27, 2024
braingram added 5 commits May 10, 2024 10:54
This changes how converters with class paths/names
as types are handled by asdf.

Prior to this commit, when converting a class instance
(foo) of class (Foo) the class path of the instance
was inspected with ``get_class_name`` and the
``_converters_by_type`` index was searched using
the class name. This index contained both classes
and class paths (as converters could use both/either)
with classes being preferred. This creates issues
when a module has a different class path from the
typical 'public' path for the class as the module
might move the private implementation without changing
the 'public' path with the expectation that this will
not break downstream code. For converters that use
class paths these types of moves will break the converter.

This commit changes the handling of class paths used
for converters so that the 'public' path can be used.
If a converter is not found for a class in
``_converters_by_type`` (which now only contains classes
as keys) then ``_converters_by_class_path`` is indexed
by checking what classes referred to by the paths
are already imported (to avoid importing every class
supported by every extension). If the class is imported
it is added to ``_converters_by_type`` and removed from
``_converters_by_class_path``.
this fixes an issue with extension order and class path.

If extension 1 is found first, and registers a converter using
a class path (with a module that hasn't been imported). Then,
extension 2 registers the same class but uses the class (not the
path). Prior to this commit (in this branch) asdf would use
extension 2 for the type (because the class path registered
with extension 1 was never indexed). With this commit all
converter class paths/types are recorded and then indexed
(in the order they're registered) fixing this issue.
@braingram braingram merged commit 097dffa into asdf-format:main May 10, 2024
47 of 48 checks passed
@braingram braingram deleted the class_path branch May 10, 2024 15:31
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.

Consider inspecting modules for class paths
2 participants