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

Commit

Permalink
Add a bit more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez authored and Matthias Koeppe committed Mar 21, 2022
1 parent f813f3c commit 196f705
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/sage/manifolds/differentiable/diff_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -1335,7 +1335,7 @@ def _init_derived(self):
"""
TensorFieldParal._init_derived(self)

def _del_derived(self, del_restrictions=True):
def _del_derived(self, del_restrictions: bool = True):
r"""
Delete the derived quantities.
Expand Down
25 changes: 17 additions & 8 deletions src/sage/manifolds/differentiable/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,23 +440,30 @@
# ****************************************************************************

from __future__ import annotations
from typing import Optional, TYPE_CHECKING
from sage.categories.manifolds import Manifolds

from typing import TYPE_CHECKING, Optional, Union

from sage.categories.homset import Hom
from sage.categories.manifolds import Manifolds
from sage.manifolds.differentiable.mixed_form_algebra import MixedFormAlgebra
from sage.manifolds.differentiable.vectorframe import VectorFrame
from sage.manifolds.manifold import TopologicalManifold
from sage.rings.cc import CC
from sage.rings.real_mpfr import RR
from sage.rings.infinity import infinity, minus_infinity
from sage.rings.integer import Integer
from sage.manifolds.manifold import TopologicalManifold
from sage.manifolds.differentiable.mixed_form_algebra import MixedFormAlgebra
from sage.rings.real_mpfr import RR

if TYPE_CHECKING:
from sage.manifolds.differentiable.diff_map import DiffMap
from sage.manifolds.differentiable.metric import PseudoRiemannianMetric
from sage.manifolds.differentiable.vectorfield_module import VectorFieldModule
from sage.manifolds.differentiable.vectorfield_module import (
VectorFieldFreeModule,
VectorFieldModule,
)

###############################################################################


class DifferentiableManifold(TopologicalManifold):
r"""
Differentiable manifold over a topological field `K`.
Expand Down Expand Up @@ -1225,7 +1232,9 @@ def tensor_bundle(self, k, l, dest_map=None):
l, dest_map=dest_map)
return self._tensor_bundles[dest_map][(k, l)]

def vector_field_module(self, dest_map: Optional[DiffMap] = None, force_free: bool = False) -> VectorFieldModule:
def vector_field_module(
self, dest_map: Optional[DiffMap] = None, force_free: bool = False
) -> Union[VectorFieldModule, VectorFieldFreeModule]:
r"""
Return the set of vector fields defined on ``self``, possibly
with values in another differentiable manifold, as a module over the
Expand Down Expand Up @@ -3029,7 +3038,7 @@ def set_change_of_frame(self, frame1, frame2, change_of_frame,
for sdom in self.open_supersets():
sdom._frame_changes[(frame2, frame1)] = change_of_frame.inverse()

def vector_frame(self, *args, **kwargs):
def vector_frame(self, *args, **kwargs) -> VectorFrame:
r"""
Define a vector frame on ``self``.
Expand Down
20 changes: 14 additions & 6 deletions src/sage/manifolds/differentiable/tensorfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
# *****************************************************************************

from __future__ import annotations

from typing import TYPE_CHECKING, Optional, Tuple, Union
from typing import TYPE_CHECKING, Optional, Tuple, TypeVar, Union

from sage.rings.integer import Integer
from sage.rings.integer_ring import ZZ
Expand All @@ -73,6 +72,9 @@


TensorType = Tuple[int, int]
T = TypeVar("T", bound="TensorField")


class TensorField(ModuleElementWithMutability):
r"""
Tensor field along a differentiable manifold.
Expand Down Expand Up @@ -406,6 +408,12 @@ class TensorField(ModuleElementWithMutability):
"""

_name: Optional[str]
_latex_name: Optional[str]
_vmodule: VectorFieldModule
_domain: DifferentiableManifold
_ambient_domain: DifferentiableManifold

def __init__(
self,
vector_field_module: VectorFieldModule,
Expand Down Expand Up @@ -616,7 +624,7 @@ def _latex_(self):
else:
return self._latex_name

def set_name(self, name=None, latex_name=None):
def set_name(self, name: Optional[str] = None, latex_name: Optional[str] = None):
r"""
Set (or change) the text name and LaTeX name of ``self``.
Expand Down Expand Up @@ -684,7 +692,7 @@ def _new_instance(self):
return type(self)(self._vmodule, self._tensor_type, sym=self._sym,
antisym=self._antisym, parent=self.parent())

def _final_repr(self, description):
def _final_repr(self, description: str) -> str:
r"""
Part of string representation common to all derived classes of
:class:`TensorField`.
Expand Down Expand Up @@ -1048,8 +1056,8 @@ def set_restriction(self, rst: TensorField):
self._is_zero = False # a priori

def restrict(
self, subdomain: DifferentiableManifold, dest_map: Optional[DiffMap] = None
) -> TensorField:
self: T, subdomain: DifferentiableManifold, dest_map: Optional[DiffMap] = None
) -> T:
r"""
Return the restriction of ``self`` to some subdomain.
Expand Down
16 changes: 10 additions & 6 deletions src/sage/manifolds/differentiable/tensorfield_paral.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,22 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional, Union

from sage.manifolds.chart import Chart
from sage.manifolds.differentiable.diff_map import DiffMap
from sage.manifolds.differentiable.manifold import DifferentiableManifold
from sage.manifolds.differentiable.tensorfield import TensorField
from sage.parallel.decorate import parallel
from sage.parallel.parallelism import Parallelism
from sage.symbolic.expression import Expression
from sage.symbolic.ring import SR
from sage.tensor.modules.free_module_tensor import FreeModuleTensor

if TYPE_CHECKING:
from sage.tensor.modules.comp import Components


class TensorFieldParal(FreeModuleTensor, TensorField):
r"""
Tensor field along a differentiable manifold, with values on a
Expand Down Expand Up @@ -713,7 +717,7 @@ def _init_derived(self):
self._extensions_graph = {self._domain: self}
self._restrictions_graph = {self._domain: self}

def _del_derived(self, del_restrictions=True):
def _del_derived(self, del_restrictions: bool = True):
r"""
Delete the derived quantities.
Expand Down Expand Up @@ -1517,7 +1521,7 @@ def paral_lie_deriv(a, b , coord_frame, chart_cp, local_list_ind):

lie_der = lie_derivative

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.
Expand Down Expand Up @@ -1746,7 +1750,7 @@ def __call__(self, *args):
# Call of the FreeModuleTensor version
return FreeModuleTensor.__call__(self_r, *args_r)

def contract(self, *args):
def contract(self, *args: Union[int, TensorField]) -> TensorFieldParal:
r"""
Contraction with another tensor field, on one or more indices.
Expand Down Expand Up @@ -2265,7 +2269,7 @@ def along(self, mapping):
comp_resu._comp[ind] = val_resu
return resu

def series_expansion(self, symbol, order):
def series_expansion(self, symbol: Expression, order: int) -> list[TensorFieldParal]:
r"""
Expand the tensor field in power series with respect to a small
parameter.
Expand Down Expand Up @@ -2410,7 +2414,7 @@ def truncate(self, symbol, order):
series = self.series_expansion(symbol, order)
return sum(symbol**i * s for i, s in enumerate(series))

def set_calc_order(self, symbol, order, truncate=False):
def set_calc_order(self, symbol: Expression, order: int, truncate: bool = False):
r"""
Trigger a power series expansion with respect to a small parameter in
computations involving the tensor field.
Expand Down
3 changes: 2 additions & 1 deletion src/sage/manifolds/differentiable/vectorframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ def set_name(self, symbol, latex_symbol=None, indices=None,
self._latex_name = r"\left({}, {}\right)".format(
self._domain._latex_name, self._latex_name)

#******************************************************************************
# ******************************************************************************


class VectorFrame(FreeModuleBasis):
r"""
Expand Down
20 changes: 15 additions & 5 deletions src/sage/manifolds/manifold.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@
from sage.categories.fields import Fields
from sage.categories.homset import Hom
from sage.categories.manifolds import Manifolds
from sage.manifolds.chart import Chart
from sage.manifolds.differentiable.chart import RealDiffChart
from sage.manifolds.differentiable.scalarfield import DiffScalarField
from sage.manifolds.scalarfield import ScalarField
from sage.manifolds.structure import (
DifferentialStructure,
RealDifferentialStructure,
Expand Down Expand Up @@ -1206,7 +1210,7 @@ def index_generator(self, nb_indices):
ind[pos] = si
ret = 1

def atlas(self):
def atlas(self) -> list[Chart]:
r"""
Return the list of charts that have been defined on the manifold.
Expand Down Expand Up @@ -1458,8 +1462,13 @@ def is_manifestly_coordinate_domain(self):
"""
return bool(self._covering_charts)

def chart(self, coordinates='', names=None, calc_method=None,
coord_restrictions=None):
def chart(
self,
coordinates: str = "",
names=None,
calc_method=None,
coord_restrictions=None,
) -> Chart:
r"""
Define a chart, the domain of which is the manifold.
Expand Down Expand Up @@ -1939,8 +1948,9 @@ def scalar_field_algebra(self):
"""
return self._scalar_field_algebra

def scalar_field(self, coord_expression=None, chart=None, name=None,
latex_name=None):
def scalar_field(
self, coord_expression=None, chart=None, name=None, latex_name=None
) -> ScalarField:
r"""
Define a scalar field on the manifold.
Expand Down
4 changes: 3 additions & 1 deletion src/sage/manifolds/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ class :class:`~sage.structure.parent.Parent`.

Element = ManifoldPoint

def __init__(self, manifold, name, latex_name=None, category=None):
_name: str

def __init__(self, manifold, name: str, latex_name=None, category=None):
r"""
Construct a manifold subset.
Expand Down
4 changes: 3 additions & 1 deletion src/sage/symbolic/function_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# version 2 or any later version. The full text of the GPL is available at:
# https://www.gnu.org/licenses/
###############################################################################
from __future__ import annotations
from typing import Union

from sage.symbolic.function import (SymbolicFunction, sfunctions_funcs,
unpickle_wrapper)
Expand Down Expand Up @@ -148,7 +150,7 @@ def unpickle_function(name, nargs, latex_name, conversions, evalf_params_first,
return function_factory(*args)


def function(s, **kwds):
def function(s, **kwds) -> Union[SymbolicFunction, list[SymbolicFunction]]:
r"""
Create a formal symbolic function with the name *s*.
Expand Down
29 changes: 21 additions & 8 deletions src/sage/tensor/modules/finite_rank_free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,20 @@ class :class:`~sage.modules.free_module.FreeModule_generic`
# the License, or (at your option) any later version.
# https://www.gnu.org/licenses/
#******************************************************************************
from __future__ import absolute_import

from sage.misc.cachefunc import cached_method
from sage.structure.unique_representation import UniqueRepresentation
from sage.structure.parent import Parent
from typing import Generator, Optional

from sage.categories.fields import Fields
from sage.categories.modules import Modules
from sage.categories.rings import Rings
from sage.categories.fields import Fields
from sage.misc.cachefunc import cached_method
from sage.rings.integer import Integer
from sage.structure.parent import Parent
from sage.structure.unique_representation import UniqueRepresentation
from sage.tensor.modules.free_module_element import FiniteRankFreeModuleElement


class FiniteRankFreeModule(UniqueRepresentation, Parent):
r"""
Free module of finite rank over a commutative ring.
Expand Down Expand Up @@ -749,6 +753,7 @@ class :class:`~sage.modules.module.Module`.
"""

Element = FiniteRankFreeModuleElement
_sindex: int

@staticmethod
def __classcall_private__(cls, ring, rank, name=None, latex_name=None, start_index=0,
Expand Down Expand Up @@ -777,8 +782,16 @@ def __classcall_private__(cls, ring, rank, name=None, latex_name=None, start_ind
return super(FiniteRankFreeModule, cls).__classcall__(
cls, ring, rank, name, latex_name, start_index, output_formatter, category)

def __init__(self, ring, rank, name=None, latex_name=None, start_index=0,
output_formatter=None, category=None):
def __init__(
self,
ring,
rank,
name=None,
latex_name=None,
start_index: int = 0,
output_formatter=None,
category=None,
):
r"""
See :class:`FiniteRankFreeModule` for documentation and examples.
Expand Down Expand Up @@ -1963,7 +1976,7 @@ def _latex_(self):
else:
return self._latex_name

def rank(self):
def rank(self) -> int:
r"""
Return the rank of the free module ``self``.
Expand Down Expand Up @@ -2083,7 +2096,7 @@ def dual(self):
"""
return self.dual_exterior_power(1)

def irange(self, start=None):
def irange(self, start: Optional[int] = None) -> Generator[int, None, None]:
r"""
Single index generator, labelling the elements of a basis of ``self``.
Expand Down
6 changes: 4 additions & 2 deletions src/sage/tensor/modules/format_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
# http://www.gnu.org/licenses/
#******************************************************************************

from sage.misc.latex import LatexExpr
from sage.structure.sage_object import SageObject


def is_atomic(expr, sep=['+', '-']):
r"""
Helper function to check whether some LaTeX expression is atomic.
Expand Down Expand Up @@ -295,7 +297,7 @@ class FormattedExpansion(SageObject):
\frac{x}{2}
"""
def __init__(self, txt=None, latex=None):
def __init__(self, txt=None, latex=None):
r"""
TESTS::
Expand All @@ -322,7 +324,7 @@ def _repr_(self):
"""
return self._txt

def _latex_(self):
def _latex_(self) -> LatexExpr:
r"""
Return a LaTeX representation of ``self``.
Expand Down
Loading

0 comments on commit 196f705

Please sign in to comment.