Skip to content

typeguard doesn't work in Colab notebookd #521

Description

@samuela

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.4.2

Python version

3.11.12

What happened?

See https://colab.research.google.com/drive/16DA2YjKcw8CMSvUgDUXl4w5TmQq47qr-?usp=sharing. In particular, I'm seeing the following error when attempting to use typeguard in a Colab notebook:

TypeError                                 Traceback (most recent call last)
[<ipython-input-1-e7fbb6fa91a6>](https://localhost:8080/#) in <cell line: 0>()
      3 from typeguard import typechecked
      4 
----> 5 @typechecked
      6 def greet(name: str) -> str:
      7     return f"Hello, {name}"

6 frames
[/usr/local/lib/python3.11/dist-packages/typeguard/_decorators.py](https://localhost:8080/#) in typechecked(target, forward_ref_policy, typecheck_fail_callback, collection_check_strategy, debug_instrumentation)
    222         target = target.__func__  # type: ignore[assignment]
    223 
--> 224     retval = instrument(target)
    225     if isinstance(retval, str):
    226         warn(

[/usr/local/lib/python3.11/dist-packages/typeguard/_decorators.py](https://localhost:8080/#) in instrument(f)
     67 
     68     target_path = [item for item in f.__qualname__.split(".") if item != "<locals>"]
---> 69     module_source = inspect.getsource(sys.modules[f.__module__])
     70     module_ast = ast.parse(module_source)
     71     instrumentor = TypeguardTransformer(target_path, f.__code__.co_firstlineno)

[/usr/lib/python3.11/inspect.py](https://localhost:8080/#) in getsource(object)
   1256     or code object.  The source code is returned as a single string.  An
   1257     OSError is raised if the source code cannot be retrieved."""
-> 1258     lines, lnum = getsourcelines(object)
   1259     return ''.join(lines)
   1260 

[/usr/lib/python3.11/inspect.py](https://localhost:8080/#) in getsourcelines(object)
   1238     raised if the source code cannot be retrieved."""
   1239     object = unwrap(object)
-> 1240     lines, lnum = findsource(object)
   1241 
   1242     if istraceback(object):

[/usr/lib/python3.11/inspect.py](https://localhost:8080/#) in findsource(object)
   1057     is raised if the source code cannot be retrieved."""
   1058 
-> 1059     file = getsourcefile(object)
   1060     if file:
   1061         # Invalidate cache if needed.

[/usr/lib/python3.11/inspect.py](https://localhost:8080/#) in getsourcefile(object)
    934     Return None if no way can be identified to get the source.
    935     """
--> 936     filename = getfile(object)
    937     all_bytecode_suffixes = importlib.machinery.DEBUG_BYTECODE_SUFFIXES[:]
    938     all_bytecode_suffixes += importlib.machinery.OPTIMIZED_BYTECODE_SUFFIXES[:]

[/usr/lib/python3.11/inspect.py](https://localhost:8080/#) in getfile(object)
    895         if getattr(object, '__file__', None):
    896             return object.__file__
--> 897         raise TypeError('{!r} is a built-in module'.format(object))
    898     if isclass(object):
    899         if hasattr(object, '__module__'):

TypeError: <module '__main__'> is a built-in module

How can we reproduce the bug?

https://colab.research.google.com/drive/16DA2YjKcw8CMSvUgDUXl4w5TmQq47qr-?usp=sharing

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions