# Buggy code
def _known_pattern(name: str, config: Config) -> Optional[Tuple[str, str]]:
    parts = name.split(".")
    module_names_to_check = (
        ".".join(parts[:first_k]) for first_k in range(len(parts), 0, -1)
    )
    for module_name_to_check in module_names_to_check:
        for pattern, placement in config.known_patterns:
            if pattern.match(module_name_to_check):
                return (placement, f"Matched configured known pattern {pattern}")

    return None


# Traceback (KeyError)
lint installed: appdirs==1.4.4,black==20.8b1,bleach==3.2.1,check-manifest==0.43,click==7.1.2,docutils==0.16,flake8==3.8.3,isort==5.5.3,mccabe==0.6.1,mypy-extensions==0.4.3,packaging==20.4,pathspec==0.8.0,pep517==0.8.2,pycodestyle==2.6.0,pyflakes==2.2.0,Pygments==2.7.1,pyparsing==2.4.7,readme-renderer==26.0,regex==2020.9.27,six==1.15.0,toml==0.10.1,typed-ast==1.4.1,typing-extensions==3.7.4.3,webencodings==0.5.1
lint run-test-pre: PYTHONHASHSEED='979047687'
lint run-test: commands[0] | flake8 src/pyramid tests setup.py
lint run-test: commands[1] | isort --check-only --df src/pyramid tests setup.py
ERROR: Unrecoverable exception thrown when parsing src/pyramid/predicates.py! This should NEVER happen.
If encountered, please open an issue: https://github.com/PyCQA/isort/issues/new
Traceback (most recent call last):
File "/home/runner/work/pyramid/pyramid/.tox/lint/bin/isort", line 8, in <module>
sys.exit(main())
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/main.py", line 886, in main
for sort_attempt in attempt_iterator:
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/main.py", line 875, in <genexpr>
sort_imports(  # type: ignore
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/main.py", line 88, in sort_imports
incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/api.py", line 264, in check_file
return check_stream(
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/api.py", line 203, in check_stream
changed: bool = sort_stream(
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/api.py", line 158, in sort_stream
changed = core.process(
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/core.py", line 326, in process
parse.file_contents(import_section, config=config),
File "/home/runner/work/pyramid/pyramid/.tox/lint/lib/python3.8/site-packages/isort/parse.py", line 474, in file_contents
straight_import |= imports[placed_module][type_of_import].get(  # type: ignore
KeyError: 'STDLIB'
ERROR: InvocationError for command /home/runner/work/pyramid/pyramid/.tox/lint/bin/isort --check-only --df src/pyramid tests setup.py (exited with code 1)
___________________________________ summary ____________________________________
ERROR:   lint: commands failed
Error: Process completed with exit code 1.

# Fixed code
def _known_pattern(name: str, config: Config) -> Optional[Tuple[str, str]]:
    parts = name.split(".")
    module_names_to_check = (
        ".".join(parts[:first_k]) for first_k in range(len(parts), 0, -1)
    )
    for module_name_to_check in module_names_to_check:
        for pattern, placement in config.known_patterns:
            if placement in config.sections and pattern.match(module_name_to_check):
                return (placement, f"Matched configured known pattern {pattern}")

    return None