New submission from Jason R. Coombs <jar...@jaraco.com>:

Attempting to define a lazy-loaded property for a module, I found [this 
guidance](https://stackoverflow.com/a/52018676/70170) referencing [module 
attribute 
access](https://docs.python.org/3/reference/datamodel.html#customizing-module-attribute-access)
 in the Python docs as a means of customizing attribute access.

I followed that guidance, but found that doctests don't have access to those 
attributes in its execution. Consider this reproducer:

```
"""
>>> print(static_property)
static value
>>> print(lazy_property)
lazy value
"""
# text.py
import types
import sys


static_property = 'static value'


class _Properties(types.ModuleType):
    @property
    def lazy_property(self):
        return 'lazy value'


sys.modules[__name__].__class__ = _Properties
```

Run that with `python -m doctest text.py` and it fails thus:

```
**********************************************************************
File "/Users/jaraco/draft/text.py", line 4, in text
Failed example:
    print(lazy_property)
Exception raised:
    Traceback (most recent call last):
      File 
"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/doctest.py", 
line 1346, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest text[1]>", line 1, in <module>
        print(lazy_property)
    NameError: name 'lazy_property' is not defined
**********************************************************************
1 items had failures:
   1 of   2 in text
***Test Failed*** 1 failures.
```

Same error using the `__getattr__` technique:

```
"""
>>> print(static_property)
static value
>>> print(lazy_property)
lazy value
"""

static_property = 'static value'


def __getattr__(name):
    if name != 'lazy_property':
        raise AttributeError(name)
    return 'lazy value'
```

I suspect the issue is that doctests runs with locals from the module's 
globals(), which won't include these lazy properties.

It would be nice if doctests could honor locals that would represent the 
properties available on the module.

----------
components: Library (Lib)
messages: 412409
nosy: jaraco
priority: normal
severity: normal
status: open
title: lazy module property not recognized by doctests
versions: Python 3.11

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue46619>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to