Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Add more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Jun 2, 2020
1 parent e620d05 commit 23b6012
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 47 deletions.
19 changes: 10 additions & 9 deletions src/sage/manifolds/differentiable/diff_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
# https://www.gnu.org/licenses/
# *****************************************************************************

from typing import Optional, TYPE_CHECKING
from sage.misc.cachefunc import cached_method
from sage.tensor.modules.free_module_alt_form import FreeModuleAltForm
from sage.manifolds.differentiable.tensorfield import TensorField
from sage.manifolds.differentiable.tensorfield_paral import TensorFieldParal

if TYPE_CHECKING:
from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule

class DiffForm(TensorField):
r"""
Expand Down Expand Up @@ -372,14 +375,13 @@ def _del_derived(self):
self.exterior_derivative.clear_cache()

@cached_method
def exterior_derivative(self):
def exterior_derivative(self) -> 'DiffForm':
r"""
Compute the exterior derivative of ``self``.
OUTPUT:
- instance of :class:`DiffForm` representing the exterior derivative
of the differential form
- the exterior derivative of the differential form
EXAMPLES:
Expand Down Expand Up @@ -573,7 +575,7 @@ def wedge(self, other):
other_r._restrictions[dom])
return resu

def degree(self):
def degree(self) -> int:
r"""
Return the degree of ``self``.
Expand Down Expand Up @@ -1134,8 +1136,8 @@ class DiffFormParal(FreeModuleAltForm, TensorFieldParal):
no symmetry; no antisymmetry
"""
def __init__(self, vector_field_module, degree, name=None,
latex_name=None):
def __init__(self, vector_field_module: 'VectorFieldModule', degree: int, name: Optional[str]=None,
latex_name: Optional[str]=None):
r"""
Construct a differential form.
Expand Down Expand Up @@ -1288,14 +1290,13 @@ def __call__(self, *args):
return TensorFieldParal.__call__(self, *args)

@cached_method
def exterior_derivative(self):
def exterior_derivative(self) -> 'DiffFormParal':
r"""
Compute the exterior derivative of ``self``.
OUTPUT:
- a :class:`DiffFormParal` representing the exterior
derivative of the differential form
- the exterior derivative of the differential form
EXAMPLES:
Expand Down
2 changes: 1 addition & 1 deletion src/sage/manifolds/differentiable/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -3707,7 +3707,7 @@ def affine_connection(self, name, latex_name=None):
AffineConnection
return AffineConnection(self, name, latex_name)

def metric(self, name, signature=None, latex_name=None, dest_map=None) -> PseudoRiemannianMetric:
def metric(self, name=None, signature=None, latex_name=None, dest_map=None) -> PseudoRiemannianMetric:
r"""
Define a pseudo-Riemannian metric on the manifold.
Expand Down
43 changes: 23 additions & 20 deletions src/sage/manifolds/differentiable/tensorfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,22 @@
# https://www.gnu.org/licenses/
# *****************************************************************************
from __future__ import print_function
from typing import Optional, Tuple, TYPE_CHECKING, Union

from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
from sage.structure.element import ModuleElement
from sage.tensor.modules.free_module_tensor import FreeModuleTensor
from sage.tensor.modules.tensor_with_indices import TensorWithIndices

if TYPE_CHECKING:
from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule
from sage.manifolds.differentiable.manifold import DifferentiableManifold
from sage.manifolds.differentiable.diff_map import DiffMap
from sage.tensor.modules.comp import Components
from sage.manifolds.differentiable.metric import PseudoRiemannianMetric

TensorType = Tuple[int, int]
class TensorField(ModuleElement):
r"""
Tensor field along a differentiable manifold.
Expand Down Expand Up @@ -115,8 +124,7 @@ class TensorField(ModuleElement):
INPUT:
- ``vector_field_module`` -- module `\mathfrak{X}(U,\Phi)` of vector
fields along `U` associated with the map `\Phi: U \rightarrow M` (cf.
:class:`~sage.manifolds.differentiable.vectorfield_module.VectorFieldModule`)
fields along `U` associated with the map `\Phi: U \rightarrow M`
- ``tensor_type`` -- pair `(k,l)` with `k` being the contravariant rank
and `l` the covariant rank
- ``name`` -- (default: ``None``) name given to the tensor field
Expand Down Expand Up @@ -362,8 +370,8 @@ class TensorField(ModuleElement):
True
"""
def __init__(self, vector_field_module, tensor_type, name=None,
latex_name=None, sym=None, antisym=None, parent=None):
def __init__(self, vector_field_module: 'VectorFieldModule', tensor_type: TensorType, name: Optional[str] = None,
latex_name: Optional[str] = None, sym=None, antisym=None, parent=None):
r"""
Construct a tensor field.
Expand Down Expand Up @@ -411,7 +419,7 @@ def __init__(self, vector_field_module, tensor_type, name=None,
parent = vector_field_module.tensor_module(*tensor_type)
ModuleElement.__init__(self, parent)
self._vmodule = vector_field_module
self._tensor_type = tuple(tensor_type)
self._tensor_type = tensor_type
self._tensor_rank = self._tensor_type[0] + self._tensor_type[1]
self._is_zero = False # a priori, may be changed below or via
# method __bool__()
Expand Down Expand Up @@ -799,14 +807,13 @@ def domain(self):
"""
return self._domain

def base_module(self):
def base_module(self) -> 'VectorFieldModule':
r"""
Return the vector field module on which ``self`` acts as a tensor.
OUTPUT:
- instance of
:class:`~sage.manifolds.differentiable.vectorfield_module.VectorFieldModule`
- the underlying vector field module
EXAMPLES:
Expand All @@ -826,7 +833,7 @@ def base_module(self):
"""
return self._vmodule

def tensor_type(self):
def tensor_type(self) -> TensorType:
r"""
Return the tensor type of ``self``.
Expand Down Expand Up @@ -907,7 +914,7 @@ def symmetries(self):

#### End of simple accessors #####

def set_restriction(self, rst):
def set_restriction(self, rst: 'TensorField'):
r"""
Define a restriction of ``self`` to some subdomain.
Expand Down Expand Up @@ -943,8 +950,6 @@ def set_restriction(self, rst):
True
"""
if not isinstance(rst, TensorField):
raise TypeError("the argument must be a tensor field")
if not rst._domain.is_subset(self._domain):
raise ValueError("the domain of the declared restriction is not " +
"a subset of the field's domain")
Expand Down Expand Up @@ -972,17 +977,15 @@ def set_restriction(self, rst):
latex_name=self._latex_name)
self._is_zero = False # a priori

def restrict(self, subdomain, dest_map=None):
def restrict(self, subdomain: 'DifferentiableManifold', dest_map: Optional['DiffMap'] = None):
r"""
Return the restriction of ``self`` to some subdomain.
If the restriction has not been defined yet, it is constructed here.
INPUT:
- ``subdomain`` --
:class:`~sage.manifolds.differentiable.manifold.DifferentiableManifold`;
open subset `U` of the tensor field domain `S`
- ``subdomain`` -- open subset `U` of the tensor field domain `S`
- ``dest_map`` --
:class:`~sage.manifolds.differentiable.diff_map.DiffMap`
(default: ``None``); destination map `\Psi:\ U \rightarrow V`,
Expand Down Expand Up @@ -1320,7 +1323,7 @@ def _add_comp_unsafe(self, basis=None):
rst = self.restrict(basis._domain, dest_map=basis._dest_map)
return rst._add_comp_unsafe(basis)

def add_comp(self, basis=None):
def add_comp(self, basis=None) -> 'Components':
r"""
Return the components of ``self`` in a given vector frame
for assignment.
Expand All @@ -1337,8 +1340,7 @@ def add_comp(self, basis=None):
OUTPUT:
- components in the given frame, as a
:class:`~sage.tensor.modules.comp.Components`; if such
- components in the given frame; if such
components did not exist previously, they are created
EXAMPLES::
Expand Down Expand Up @@ -1785,6 +1787,7 @@ def display(self, frame=None, chart=None):
except AttributeError:
# case of a genuine vector frame
pass

rst = self.restrict(frame._domain, dest_map=frame._dest_map)
return rst.display(frame, chart)

Expand Down Expand Up @@ -3101,7 +3104,7 @@ def trace(self, pos1=0, pos2=1):
resu._restrictions[rst._domain] = rst
return resu

def contract(self, *args):
def contract(self, *args) -> 'TensorField':
r"""
Contraction of ``self`` with another tensor field on one or
more indices.
Expand Down
8 changes: 5 additions & 3 deletions src/sage/manifolds/differentiable/tensorfield_paral.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,9 @@
from sage.parallel.decorate import parallel
from sage.parallel.parallelism import Parallelism
from sage.symbolic.ring import SR
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sage.tensor.modules.comp import Components

class TensorFieldParal(FreeModuleTensor, TensorField):
r"""
Expand Down Expand Up @@ -1084,7 +1087,7 @@ class :class:`~sage.tensor.modules.comp.Components`; if such
# The add_comp operation is performed on the subdomain:
return rst.add_comp(basis=basis)

def comp(self, basis=None, from_basis=None):
def comp(self, basis=None, from_basis=None) -> 'Components':
r"""
Return the components in a given vector frame.
Expand All @@ -1102,8 +1105,7 @@ def comp(self, basis=None, from_basis=None):
OUTPUT:
- components in the vector frame ``basis``, as an instance of the
class :class:`~sage.tensor.modules.comp.Components`
- components in the vector frame ``basis``
EXAMPLES::
Expand Down
23 changes: 10 additions & 13 deletions src/sage/manifolds/differentiable/vectorfield_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#******************************************************************************

from sage.manifolds.differentiable.diff_map import DiffMap
from sage.manifolds.differentiable.manifold import DifferentiableManifold
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from sage.categories.modules import Modules
Expand All @@ -48,6 +47,9 @@
from sage.tensor.modules.finite_rank_free_module import FiniteRankFreeModule
from sage.manifolds.differentiable.vectorfield import (VectorField,
VectorFieldParal)
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from sage.manifolds.differentiable.manifold import DifferentiableManifold

class VectorFieldModule(UniqueRepresentation, Parent):
r"""
Expand Down Expand Up @@ -183,7 +185,7 @@ class VectorFieldModule(UniqueRepresentation, Parent):
"""
Element = VectorField

def __init__(self, domain: DifferentiableManifold, dest_map: DiffMap=None):
def __init__(self, domain: 'DifferentiableManifold', dest_map: DiffMap=None):
r"""
Construct the module of vector fields taking values on a (a priori)
non-parallelizable differentiable manifold.
Expand Down Expand Up @@ -382,7 +384,7 @@ def _latex_(self):
else:
return self._latex_name

def domain(self):
def domain(self) -> 'DifferentiableManifold':
r"""
Return the domain of the vector fields in this module.
Expand All @@ -391,10 +393,7 @@ def domain(self):
OUTPUT:
- instance of
:class:`~sage.manifolds.differentiable.manifold.DifferentiableManifold`
representing the domain of the vector fields that belong to this
module
- the domain of the vector fields that belong to this module
EXAMPLES::
Expand All @@ -411,7 +410,7 @@ def domain(self):
"""
return self._domain

def ambient_domain(self):
def ambient_domain(self) -> 'DifferentiableManifold':
r"""
Return the manifold in which the vector fields of this module take
their values.
Expand All @@ -421,9 +420,7 @@ def ambient_domain(self):
OUTPUT:
- instance of
:class:`~sage.manifolds.differentiable.manifold.DifferentiableManifold`
representing the manifold in which the vector fields of this
- the manifold in which the vector fields of this
module take their values
EXAMPLES::
Expand Down Expand Up @@ -1541,7 +1538,7 @@ def _repr_(self):
" mapped into the {}".format(self._ambient_domain)
return description

def domain(self) -> DifferentiableManifold:
def domain(self) -> 'DifferentiableManifold':
r"""
Return the domain of the vector fields in ``self``.
Expand Down Expand Up @@ -1570,7 +1567,7 @@ def domain(self) -> DifferentiableManifold:
"""
return self._domain

def ambient_domain(self) -> DifferentiableManifold:
def ambient_domain(self) -> 'DifferentiableManifold':
r"""
Return the manifold in which the vector fields of ``self``
take their values.
Expand Down
4 changes: 3 additions & 1 deletion src/sage/tensor/modules/free_module_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class being:
# https://www.gnu.org/licenses/
# *****************************************************************************
from __future__ import absolute_import
from typing import Dict

from sage.rings.integer import Integer
from sage.structure.element import ModuleElement
Expand All @@ -202,6 +203,7 @@ class being:
from sage.parallel.decorate import parallel
from sage.parallel.parallelism import Parallelism
from sage.manifolds.chart import Chart
from sage.tensor.modules.free_module_basis import FreeModuleBasis

# TODO: remove the import of Chart after _preparse_display has been redefined
# in tensor fields
Expand Down Expand Up @@ -292,7 +294,7 @@ def __init__(self, fmodule, tensor_type, name=None, latex_name=None,
self._latex_name = self._name
else:
self._latex_name = latex_name
self._components = {} # dict. of the sets of components on various
self._components:Dict[FreeModuleBasis, Components] = {} # dict. of the sets of components on various
# bases, with the bases as keys (initially empty)

# Treatment of symmetry declarations:
Expand Down

0 comments on commit 23b6012

Please sign in to comment.