On Wed, Oct 11, 2023 at 11:50 PM Aaron Meurer <asmeu...@gmail.com> wrote:

> Is there a way to make pickle not depend on the specific submodule
> that a class is defined in?


No. `Unpickler` somehow has to locate the class/reconstruction function. It
will have to use the name given by the `__reduce_ex__` during pickling.


> Wouldn't this happen again if you ever
> decided to rename _core.
>

Yes.


> The underscores in numpy._core._reconstruct don't actually do anything
> here in terms of making the interface not public, and if anything, are
> really misleading.
>

There's private and there's private. There are two broad things that could
mean:

1. We don't want users playing around with it, importing it directly in
their code and using it.
2. Marking that we won't mess around with it without going through the
deprecation policy.

Usually, these two go together (hiding it from users directly calling it
means that we get to mess around with it freely), but when we get to
metaprogramming tasks like pickling, they become a little decoupled.
Because the "user" that we have to think about isn't a person reading
documentation and browsing APIs, but a file that someone created years ago
and some infrastructure that interprets that file. I think it's good that
`_reconstruct` is hidden from human users and is distinct from almost all
of what constitutes "numpy's public API", but that doesn't mean that we
can't have a tiny third class of functions where we do preserve some
guarantees. It's only occasionally confusing to us core devs, not users
(who can rely on the "don't touch underscored names" rule just fine).

I'm also curious about this more generally. We tend to think of the
> fully qualified name of a class as being an implementation detail for
> many libraries and only the top-level lib.Name should be used, but
> many things in the language (including this) break this.
>

Yes, and it's why these things attempt to be scoped in ways that limit this
problem. I.e. if you use pickling, you're told to use it only for transient
data with the same versions of libraries on both ends of the pipe, but the
reality is that it's too useful to avoid in creating files with arbitrarily
long lives. Not their fault; they warned us!

-- 
Robert Kern
_______________________________________________
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com

Reply via email to