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: Allow redefinition of names in guppy modules #326

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 3 additions & 9 deletions guppylang/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from types import ModuleType
from typing import Any, Union

from guppylang.ast_util import AstNode
from guppylang.checker.core import Globals, PyScope
from guppylang.compiler.core import CompiledGlobals
from guppylang.definition.common import (
Expand Down Expand Up @@ -104,14 +103,16 @@ def load(self, m: Union[ModuleType, "GuppyModule"]) -> None:
def register_def(self, defn: RawDef, instance: TypeDef | None = None) -> None:
"""Registers a definition with this module.

If the name of the definition is already defined, the new definition
replaces the old.

Optionally, the definition can be marked as an instance method by passing the
corresponding instance type definition.
"""
self._check_not_yet_compiled()
if self._instance_func_buffer is not None and not isinstance(defn, TypeDef):
self._instance_func_buffer[defn.name] = defn
else:
self._check_name_available(defn.name, defn.defined_at)
if isinstance(defn, TypeDef | ParamDef):
self._raw_type_defs[defn.id] = defn
else:
Expand Down Expand Up @@ -228,13 +229,6 @@ 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 self.contains(name):
raise GuppyError(
f"Module `{self.name}` already contains a definition named `{name}`",
node,
)


def get_py_scope(f: PyFunc) -> PyScope:
"""Returns a mapping of all variables captured by a Python function.
Expand Down
19 changes: 19 additions & 0 deletions tests/integration/test_redefinition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from guppylang.decorator import guppy
from guppylang.module import GuppyModule

import guppylang.prelude.quantum as quantum


def test_redefinition(validate):
module = GuppyModule("test")
module.load(quantum)

@guppy(module)
def test() -> bool:
return True

@guppy(module)
def test() -> bool: # noqa: F811
return False

validate(module.compile())
Loading