# Buggy code
def read_pyproject_toml(
    ctx: click.Context, param: click.Parameter, value: Optional[str]
) -> Optional[str]:
    """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.

    Returns the path to a successfully found and read configuration file, None
    otherwise.
    """
    if not value:
        value = find_pyproject_toml(ctx.params.get("src", ()))
        if value is None:
            return None

    try:
        config = parse_pyproject_toml(value)
    except (toml.TomlDecodeError, OSError) as e:
        raise click.FileError(
            filename=value, hint=f"Error reading configuration file: {e}"
        )

    if not config:
        return None

    target_version = config.get("target_version")
    if target_version is not None and not isinstance(target_version, list):
        raise click.BadOptionUsage(
            "target-version", "Config key target-version must be a list"
        )

    default_map: Dict[str, Any] = {}
    if ctx.default_map:
        default_map.update(ctx.default_map)
    default_map.update(config)

    ctx.default_map = default_map
    return value


# Traceback (ImportError)
$ python -m unittest
E....[2020-06-14 14:22:15,341] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,342] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,680] DEBUG: Using selector: EpollSelector (selector_events.py:59)
.[2020-06-14 14:22:15,682] DEBUG: Using selector: EpollSelector (selector_events.py:59)
[2020-06-14 14:22:15,683] INFO: 16 projects to run Black over (lib.py:311)
[2020-06-14 14:22:15,683] DEBUG: Using 2 parallel workers to run Black (lib.py:316)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on aioexabgp (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on attrs (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on bandersnatch (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on channels (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on django (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping django as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on flake8-bugbear (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on hypothesis (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pandas (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping pandas as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on poetry (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on ptr (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pyramid (lib.py:247)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on pytest (lib.py:247)
[2020-06-14 14:22:15,684] INFO: Skipping pytest as it's disabled via config (lib.py:254)
[2020-06-14 14:22:15,684] DEBUG: worker 0 working on sqlalchemy (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on tox (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on virtualenv (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: worker 0 working on warehouse (lib.py:247)
[2020-06-14 14:22:15,685] DEBUG: project_runner 0 exiting (lib.py:245)
[2020-06-14 14:22:15,685] DEBUG: project_runner 1 exiting (lib.py:245)
[2020-06-14 14:22:15,685] INFO: Analyzing results (lib.py:327)
.
======================================================================
ERROR: tests.test_black (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests.test_black
Traceback (most recent call last):
File "/home/username/miniconda3/envs/ml4sts/lib/python3.8/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/home/username/miniconda3/envs/ml4sts/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
__import__(name)
File "/home/erik/black/tests/test_black.py", line 1766, in <module>
class BlackDTestCase(AioHTTPTestCase):
NameError: name 'AioHTTPTestCase' is not defined


----------------------------------------------------------------------
Ran 9 tests in 0.348s

FAILED (errors=1)

# Fixed code
def read_pyproject_toml(
    ctx: click.Context, param: click.Parameter, value: Optional[str]
) -> Optional[str]:
    """Inject Black configuration from "pyproject.toml" into defaults in `ctx`.

    Returns the path to a successfully found and read configuration file, None
    otherwise.
    """
    if not value:
        value = find_pyproject_toml(ctx.params.get("src", ()))
        if value is None:
            return None

    try:
        config = parse_pyproject_toml(value)
    except (toml.TomlDecodeError, OSError) as e:
        raise click.FileError(
            filename=value, hint=f"Error reading configuration file: {e}"
        )

    if not config:
        return None
    else:
        # Sanitize the values to be Click friendly. For more information please see:
        # https://github.com/psf/black/issues/1458
        # https://github.com/pallets/click/issues/1567
        config = {
            k: str(v) if not isinstance(v, (list, dict)) else v
            for k, v in config.items()
        }

    target_version = config.get("target_version")
    if target_version is not None and not isinstance(target_version, list):
        raise click.BadOptionUsage(
            "target-version", "Config key target-version must be a list"
        )

    default_map: Dict[str, Any] = {}
    if ctx.default_map:
        default_map.update(ctx.default_map)
    default_map.update(config)

    ctx.default_map = default_map
    return value