# Buggy code
def is_cython_func(func: Callable) -> bool:
    """Slightly hacky check for whether a callable is implemented in Cython.
    Can be used to implement slightly different behaviors, especially around
    inspecting and parameter annotations. Note that this will only return True
    for actual cdef functions and methods, not regular Python functions defined
    in Python modules.

    func (Callable): The callable to check.
    RETURNS (bool): Whether the callable is Cython (probably).
    """
    attr = "__pyx_vtable__"
    if hasattr(func, attr):  # function or class instance
        return True
    # https://stackoverflow.com/a/55767059
    if hasattr(func, "__qualname__") and hasattr(func, "__module__"):  # method
        cls_func = vars(sys.modules[func.__module__])[func.__qualname__.split(".")[0]]
        return hasattr(cls_func, attr)
    return False


# Traceback (KeyError)
$ python -m spacy train test.cfg --code mycode.py
ℹ Using CPU

=========================== Initializing pipeline ===========================
Set up nlp object from config
Pipeline: ['lemmatizer']
Created vocabulary
Finished initializing nlp object
Traceback (most recent call last):
File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/__main__.py", line 4, in <module>
setup_cli()
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/cli/_util.py", line 68, in setup_cli
command(prog_name=COMMAND)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/typer/main.py", line 497, in wrapper
return callback(**use_params)  # type: ignore
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/cli/train.py", line 56, in train_cli
nlp = init_nlp(config, use_gpu=use_gpu)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/training/initialize.py", line 70, in init_nlp
nlp.initialize(lambda: train_corpus(nlp), sgd=optimizer)
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/language.py", line 1243, in initialize
p_settings = validate_init_settings(
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/schemas.py", line 128, in validate_init_settings
schema = get_arg_model(func, exclude=exclude, name="InitArgModel")
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/schemas.py", line 100, in get_arg_model
default_empty = None if is_cython_func(func) else ...
File "/home/antti/spacy-custom-code/.venv/lib/python3.9/site-packages/spacy/util.py", line 1458, in is_cython_func
cls_func = vars(sys.modules[func.__module__])[func.__qualname__.split(".")[0]]
KeyError: 'python_code'

# Fixed code
def is_cython_func(func: Callable) -> bool:
    """Slightly hacky check for whether a callable is implemented in Cython.
    Can be used to implement slightly different behaviors, especially around
    inspecting and parameter annotations. Note that this will only return True
    for actual cdef functions and methods, not regular Python functions defined
    in Python modules.

    func (Callable): The callable to check.
    RETURNS (bool): Whether the callable is Cython (probably).
    """
    attr = "__pyx_vtable__"
    if hasattr(func, attr):  # function or class instance
        return True
    # https://stackoverflow.com/a/55767059
    if (
        hasattr(func, "__qualname__")
        and hasattr(func, "__module__")
        and func.__module__ in sys.modules
    ):  # method
        cls_func = vars(sys.modules[func.__module__])[func.__qualname__.split(".")[0]]
        return hasattr(cls_func, attr)
    return False