abalkin / pytest-leaks

A pytest plugin to trace resource leaks.
https://abalkin.github.io/pytest-leaks
Other
115 stars 4 forks source link

Doctests failed with name not defined #8

Closed romantolkachyov closed 5 years ago

romantolkachyov commented 6 years ago

When I run pytest with --leaks option, I've run into the multiple issues with doctest. For example, this doctest will fail:

import yaml
from attrdict import AttrDict

class BaseConfig(AttrDict):

    """Base configuration.

    Used in grabber and store service.

    Handle application configuration.

    Load from file:

    >>> c = BaseConfig('testing.yml')
    >>> c.app.debug
    True

    Load from dict:

    >>> c = BaseConfig({
    ...     'app': {
    ...         'debug': True
    ...     }
    ... })
    >>> c.app.debug
    True
    """

    def __init__(self, config):
        if isinstance(config, dict):
            super().__init__(config)
        elif isinstance(config, str):
            with open(config) as fp:
                data = yaml.safe_load(fp)
                super().__init__(data)
        else:
            raise ValueError("Unsupported configuration format")

Exception was:

007 Base configuration.
008
009     Used in grabber and store service.
010
011     Handle application configuration. It is a better to write getter method
012     instead of direct settings access.
013
014     Load from file:
015
016     >>> c = BaseConfig('testing.yml')
UNEXPECTED EXCEPTION: NameError("name 'BaseConfig' is not defined",)
Traceback (most recent call last):

  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/doctest.py", line 1330, in __run
    compileflags, 1), test.globs)

  File "<doctest newsutils.config.BaseConfig[0]>", line 1, in <module>

NameError: name 'BaseConfig' is not defined