Skip to content

Commit

Permalink
get mypy to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
dakinggg authored and svlandeg committed Mar 22, 2024
1 parent 7cc7669 commit 643c116
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions catalogue/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Sequence, Any, Dict, Tuple, Callable, Optional, TypeVar, Union, Generic
from types import ModuleType, MethodType, FunctionType, TracebackType, FrameType, CodeType
from typing import List
import inspect

Expand Down Expand Up @@ -158,6 +159,9 @@ def find(self, name: str) -> Dict[str, Optional[Union[str, int]]]:
line_no: Optional[int] = None
file_name: Optional[str] = None
try:
if not isinstance(func, (ModuleType, MethodType, FunctionType, TracebackType, FrameType, CodeType, type)):
raise TypeError(f"func type {type(func)} is not a valid type for inspect.getsourcelines()")

_, line_no = inspect.getsourcelines(func)
file_name = inspect.getfile(func)
except (TypeError, ValueError):
Expand Down
28 changes: 28 additions & 0 deletions catalogue/tests/test_catalogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,31 @@ def a():
assert info["file"] == str(Path(__file__))
assert info["docstring"] == "This is a registered function."
assert info["line_no"]

def test_registry_find_module():
import json

test_registry = catalogue.create("test_registry_find_module")

test_registry.register("json", func=json)

info = test_registry.find("json")
assert info["module"] == "json"
assert info["file"] == json.__file__
assert info["docstring"] == json.__doc__.strip('\n')
assert info["line_no"] == 0

def test_registry_find_class():
test_registry = catalogue.create("test_registry_find_class")

class TestClass:
"""This is a registered class."""
pass

test_registry.register("test_class", func=TestClass)

info = test_registry.find("test_class")
assert info["module"] == "catalogue.tests.test_catalogue"
assert info["file"] == str(Path(__file__))
assert info["docstring"] == TestClass.__doc__
assert info["line_no"]

0 comments on commit 643c116

Please sign in to comment.