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

Remove an import-time load of sample text. #21

Closed
wants to merge 1 commit into from

Conversation

sputt
Copy link

@sputt sputt commented Dec 27, 2024

Remove some sample text that is not needed for the operation of jaraco.text.

@sputt
Copy link
Author

sputt commented Dec 27, 2024

Including the sample text I presume was useful for following docstring examples, but has two disadvantages:

  1. Installers that handle spaces poorly may fail
  2. This is I/O that is not useful that occurs on every pkg_resources import in the world.

For example, this stack trace from a Bazel build using setuptools:

  File "req_compile/metadata/metadata.py", line 7, in <module>
    import pkg_resources
  File "/site-packages/pkg_resources/__init__.py", line 90, in <module>
    from jaraco.text import drop_comment, join_continuation, yield_lines
  File "/site-packages/setuptools/_vendor/jaraco/text/__init__.py", line 231, in <module>
    files(__name__).joinpath('Lorem ipsum.txt').read_text(encoding='utf-8')
  File "lib/python3.11/pathlib.py", line 1058, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "lib/python3.11/pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '/site-packages/setuptools/_vendor/jaraco/text/Lorem ipsum.txt'

This installer failed to place the datafile, so failed at runtime. However, the symbols package resources wanted were "drop_comment, join_continuation, yield_lines", which have no need for such a datafile.

Let's remove it completely.

@sputt
Copy link
Author

sputt commented Jan 10, 2025

Ping @jaraco. This will also need to be vendored into setuptools

@jaraco
Copy link
Owner

jaraco commented Jan 20, 2025

See #11 (comment), where I describe the motivation for this attribute and its purpose. As you've observed, removing this attribute does cause the tests to fail, but it also removes a public attribute that could cause other applications to fail as well.

I have considered making the attribute lazy to load. If only Python had a built-in mechanism to make an attribute lazy in an elegant way, something like:

lorem_ipsum = LazyAttribute(lambda: files(__name__).joinpath('Lorem ipsum.txt').read_text(encoding='utf-8'))

Unfortunately, it doesn't, and the best thing we have is to define __getattr__ on the module (and all the wrangling that entails). I'd like to avoid that unless there's a re-usable technique to keep the wrangling encapsulated.

Here's what I'm thinking:

from jaraco.modules import LazyAttribute
...
lorem_ipsum = LazyAttribute(lambda: ...)
...
# end of module
LazyAttribute.enroll()

Then, LazyAttribute would simply store the callable, and LazyAttribute.enroll() would inspect the call stack to locate the module, all LazyAttribute objects in it (and their names), remove them from the namespace, and replace them with caching accessors in <module>.__getattr__.

Of course, jaraco.modules.LazyAttribute doesn't exist. Maybe there's already some prior art for this.

@bswck Does any of your work on lazy loading of modules provide the functionality to lazy load attributes? Have you seen any other libraries that can help facilitate this need? Can you think of any better way to avoid the end-of-module enrollment hook?

@jaraco
Copy link
Owner

jaraco commented Jan 20, 2025

Since I'm not going to accept this change, I'm closing this issue and moving the discussion to a new issue.

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