Skip to content

Commit

Permalink
Remove deprecated functionality (attach_operators_from_hash_data). (#220
Browse files Browse the repository at this point in the history
)

* Remove deprecated functionality (attach_operators_from_hash_data).

The deprecation was introduced 4 months ago (#168).
Since all the core objects of UFL is using it (Mesh and FunctionSpace) it means that pytest is throwing tons of deprecation warnings at import:
```python
python3 -W error::DeprecationWarning -c "import ufl.Mesh"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/root/shared/ufl/__init__.py", line 265, in <module>
    from ufl.domain import as_domain, AbstractDomain, Mesh, MeshView, TensorProductMesh
  File "/root/shared/ufl/domain.py", line 190, in <module>
    class TensorProductMesh(AbstractDomain):
  File "/root/shared/ufl/core/ufl_type.py", line 56, in attach_operators_from_hash_data
    warnings.warn("attach_operators_from_hash_data deprecated, please use UFLObject instead.", DeprecationWarning)
DeprecationWarning: attach_operators_from_hash_data deprecated, please use UFLObject instead.
```

* Flake8

* implement __str__ for function spaces

---------

Co-authored-by: Matthew Scroggs <[email protected]>
  • Loading branch information
jorgensd and mscroggs authored Oct 16, 2023
1 parent ff3f69f commit 4b6142a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 66 deletions.
8 changes: 3 additions & 5 deletions test/test_strip_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ufl import Coefficient, Constant, FunctionSpace, Mesh, TestFunction, TrialFunction, dx, grad, inner, triangle
from ufl.algorithms import replace_terminal_data, strip_terminal_data
from ufl.core.ufl_id import attach_ufl_id
from ufl.core.ufl_type import attach_operators_from_hash_data
from ufl.core.ufl_type import UFLObject
from ufl.finiteelement import FiniteElement
from ufl.pullback import identity_pullback
from ufl.sobolevspace import H1
Expand All @@ -13,16 +13,14 @@
"""The minimum value returned by sys.getrefcount."""


@attach_operators_from_hash_data
@attach_ufl_id
class AugmentedMesh(Mesh):
class AugmentedMesh(Mesh, UFLObject):
def __init__(self, *args, data):
super().__init__(*args)
self.data = data


@attach_operators_from_hash_data
class AugmentedFunctionSpace(FunctionSpace):
class AugmentedFunctionSpace(FunctionSpace, UFLObject):
def __init__(self, *args, data):
super().__init__(*args)
self.data = data
Expand Down
2 changes: 0 additions & 2 deletions ufl/core/ufl_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def init_ufl_id(self, ufl_id):
return init_ufl_id

# Modify class:
if hasattr(cls, "__slots__"):
assert "_ufl_id" in cls.__slots__
cls._ufl_global_id = 0
cls.ufl_id = _get_ufl_id
cls._init_ufl_id = _init_ufl_id(cls)
Expand Down
29 changes: 0 additions & 29 deletions ufl/core/ufl_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
from __future__ import annotations

import typing
import warnings
from abc import ABC, abstractmethod

# Avoid circular import
import ufl.core as core
from ufl.core.compute_expr_hash import compute_expr_hash
from ufl.utils.formatting import camel2underscore
Expand Down Expand Up @@ -48,33 +46,6 @@ def __ne__(self, other):
return not self.__eq__(other)


def attach_operators_from_hash_data(cls):
"""Class decorator to attach ``__hash__``, ``__eq__`` and ``__ne__`` implementations.
These are implemented in terms of a ``._ufl_hash_data()`` method on the class,
which should return a tuple or hashable and comparable data.
"""
warnings.warn("attach_operators_from_hash_data deprecated, please use UFLObject instead.", DeprecationWarning)
assert hasattr(cls, "_ufl_hash_data_")

def __hash__(self):
"""__hash__ implementation attached in attach_operators_from_hash_data."""
return hash(self._ufl_hash_data_())
cls.__hash__ = __hash__

def __eq__(self, other):
"""__eq__ implementation attached in attach_operators_from_hash_data."""
return type(self) is type(other) and self._ufl_hash_data_() == other._ufl_hash_data_()
cls.__eq__ = __eq__

def __ne__(self, other):
"""__ne__ implementation attached in attach_operators_from_hash_data."""
return not self.__eq__(other)
cls.__ne__ = __ne__

return cls


def get_base_attr(cls, name):
"""Return first non-``None`` attribute of given name among base classes."""
for base in cls.mro():
Expand Down
8 changes: 3 additions & 5 deletions ufl/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ufl.cell import AbstractCell
from ufl.core.ufl_id import attach_ufl_id
from ufl.core.ufl_type import attach_operators_from_hash_data
from ufl.core.ufl_type import UFLObject
from ufl.corealg.traversal import traverse_unique_terminals
from ufl.sobolevspace import H1

Expand Down Expand Up @@ -52,9 +52,8 @@ def topological_dimension(self):
# AbstractDomain.__init__(self, geometric_dimension, geometric_dimension)


@attach_operators_from_hash_data
@attach_ufl_id
class Mesh(AbstractDomain):
class Mesh(AbstractDomain, UFLObject):
"""Symbolic representation of a mesh."""

def __init__(self, coordinate_element, ufl_id=None, cargo=None):
Expand Down Expand Up @@ -122,9 +121,8 @@ def _ufl_sort_key_(self):
"Mesh", typespecific)


@attach_operators_from_hash_data
@attach_ufl_id
class MeshView(AbstractDomain):
class MeshView(AbstractDomain, UFLObject):
"""Symbolic representation of a mesh."""

def __init__(self, mesh, topological_dimension, ufl_id=None):
Expand Down
53 changes: 28 additions & 25 deletions ufl/functionspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Modified by Massimiliano Leoni, 2016
# Modified by Cecile Daversin-Catty, 2018

from ufl.core.ufl_type import attach_operators_from_hash_data
from ufl.core.ufl_type import UFLObject
from ufl.domain import join_domains
from ufl.duals import is_dual, is_primal

Expand All @@ -29,13 +29,11 @@ class AbstractFunctionSpace(object):
def ufl_sub_spaces(self):
"""Return ufl sub spaces."""
raise NotImplementedError(
"Missing implementation of IFunctionSpace.ufl_sub_spaces in %s."
% self.__class__.__name__
f"Missing implementation of ufl_sub_spaces in {self.__class__.__name__}."
)


@attach_operators_from_hash_data
class BaseFunctionSpace(AbstractFunctionSpace):
class BaseFunctionSpace(AbstractFunctionSpace, UFLObject):
"""Base function space."""

def __init__(self, domain, element):
Expand Down Expand Up @@ -109,13 +107,10 @@ def _ufl_signature_data_(self, renumbering, name=None):

def __repr__(self):
"""Representation."""
r = "BaseFunctionSpace(%s, %s)" % (repr(self._ufl_domain),
repr(self._ufl_element))
return r
return f"BaseFunctionSpace({self._ufl_domain!r}, {self._ufl_element!r})"


@attach_operators_from_hash_data
class FunctionSpace(BaseFunctionSpace):
class FunctionSpace(BaseFunctionSpace, UFLObject):
"""Representation of a Function space."""

_primal = True
Expand All @@ -135,13 +130,14 @@ def _ufl_signature_data_(self, renumbering):

def __repr__(self):
"""Representation."""
r = "FunctionSpace(%s, %s)" % (repr(self._ufl_domain),
repr(self._ufl_element))
return r
return f"FunctionSpace({self._ufl_domain!r}, {self._ufl_element!r})"

def __str__(self):
"""String."""
return f"FunctionSpace({self._ufl_domain}, {self._ufl_element})"

@attach_operators_from_hash_data
class DualSpace(BaseFunctionSpace):

class DualSpace(BaseFunctionSpace, UFLObject):
"""Representation of a Dual space."""

_primal = False
Expand All @@ -165,13 +161,14 @@ def _ufl_signature_data_(self, renumbering):

def __repr__(self):
"""Representation."""
r = "DualSpace(%s, %s)" % (repr(self._ufl_domain),
repr(self._ufl_element))
return r
return f"DualSpace({self._ufl_domain!r}, {self._ufl_element!r})"

def __str__(self):
"""String."""
return f"DualSpace({self._ufl_domain}, {self._ufl_element})"


@attach_operators_from_hash_data
class TensorProductFunctionSpace(AbstractFunctionSpace):
class TensorProductFunctionSpace(AbstractFunctionSpace, UFLObject):
"""Tensor product function space."""

def __init__(self, *function_spaces):
Expand All @@ -196,12 +193,14 @@ def _ufl_signature_data_(self, renumbering):

def __repr__(self):
"""Representation."""
r = "TensorProductFunctionSpace(*%s)" % repr(self._ufl_function_spaces)
return r
return f"TensorProductFunctionSpace(*{self._ufl_function_spaces!r})"

def __str__(self):
"""String."""
return self.__repr__()

@attach_operators_from_hash_data
class MixedFunctionSpace(AbstractFunctionSpace):

class MixedFunctionSpace(AbstractFunctionSpace, UFLObject):
"""Mixed function space."""

def __init__(self, *args):
Expand Down Expand Up @@ -297,4 +296,8 @@ def _ufl_signature_data_(self, renumbering):

def __repr__(self):
"""Representation."""
return f"MixedFunctionSpace(*{self._ufl_function_spaces})"
return f"MixedFunctionSpace(*{self._ufl_function_spaces!r})"

def __str__(self):
"""String."""
return self.__repr__()

0 comments on commit 4b6142a

Please sign in to comment.