Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Utility methods to check if a module contains a fn/type #92

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion guppy/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,20 @@ def compile(self) -> Hugr | None:
self._compiled = True
return graph

def contains_function(self, name: str) -> bool:
"""Returns 'True' if the module contains a function with the given name."""
return name in self._func_defs or name in self._custom_funcs

def contains_type(self, name: str) -> bool:
"""Returns 'True' if the module contains a type with the given name."""
return name in self._globals.types or name in self._globals.type_vars

def _check_not_yet_compiled(self) -> None:
if self._compiled:
raise GuppyError(f"The module `{self.name}` has already been compiled")

def _check_name_available(self, name: str, node: AstNode | None) -> None:
if name in self._func_defs or name in self._custom_funcs:
if self.contains_function(name):
raise GuppyError(
f"Module `{self.name}` already contains a function named `{name}`",
node,
Expand Down