|
22 | 22 | from pyccel.ast.basic import PyccelAstNode, TypedAstNode, ScopedAstNode
|
23 | 23 |
|
24 | 24 | from pyccel.ast.builtins import PythonPrint, PythonTupleFunction, PythonSetFunction
|
25 |
| -from pyccel.ast.builtins import PythonComplex, PythonDict |
| 25 | +from pyccel.ast.builtins import PythonComplex, PythonDict, PythonDictFunction |
26 | 26 | from pyccel.ast.builtins import builtin_functions_dict, PythonImag, PythonReal
|
27 | 27 | from pyccel.ast.builtins import PythonList, PythonConjugate , PythonSet
|
28 | 28 | from pyccel.ast.builtins import (PythonRange, PythonZip, PythonEnumerate,
|
|
65 | 65 | from pyccel.ast.datatypes import PythonNativeBool, PythonNativeInt, PythonNativeFloat
|
66 | 66 | from pyccel.ast.datatypes import DataTypeFactory, PrimitiveFloatingPointType
|
67 | 67 | from pyccel.ast.datatypes import InhomogeneousTupleType, HomogeneousTupleType, HomogeneousSetType, HomogeneousListType
|
68 |
| -from pyccel.ast.datatypes import PrimitiveComplexType, FixedSizeNumericType |
| 68 | +from pyccel.ast.datatypes import PrimitiveComplexType, FixedSizeNumericType, DictType |
69 | 69 |
|
70 | 70 | from pyccel.ast.functionalexpr import FunctionalSum, FunctionalMax, FunctionalMin, GeneratorComprehension, FunctionalFor
|
71 | 71 |
|
|
149 | 149 | type_container = {
|
150 | 150 | PythonTupleFunction : HomogeneousTupleType,
|
151 | 151 | PythonList : HomogeneousListType,
|
152 |
| - PythonSetFunction : HomogeneousSetType |
| 152 | + PythonSetFunction : HomogeneousSetType, |
153 | 153 | }
|
154 | 154 |
|
155 | 155 | #==============================================================================
|
@@ -1640,7 +1640,7 @@ def _ensure_inferred_type_matches_existing(self, class_type, d_var, var, is_auga
|
1640 | 1640 | if raise_error:
|
1641 | 1641 | name = var.name
|
1642 | 1642 | rhs_str = str(rhs)
|
1643 |
| - errors.report(INCOMPATIBLE_TYPES_IN_ASSIGNMENT.format(repr(var.class_type), repr(class_type)), |
| 1643 | + errors.report(INCOMPATIBLE_TYPES_IN_ASSIGNMENT.format(var.class_type, class_type), |
1644 | 1644 | symbol=f'{name}={rhs_str}',
|
1645 | 1645 | bounding_box=(self.current_ast_node.lineno, self.current_ast_node.col_offset),
|
1646 | 1646 | severity='error')
|
@@ -1880,6 +1880,30 @@ def _find_superclasses(self, expr):
|
1880 | 1880 |
|
1881 | 1881 | return list(parent.values())
|
1882 | 1882 |
|
| 1883 | + def _convert_syntactic_object_to_type_annotation(self, syntactic_annotation): |
| 1884 | + """ |
| 1885 | + Convert an arbitrary syntactic object to a type annotation. |
| 1886 | +
|
| 1887 | + Convert an arbitrary syntactic object to a type annotation. This means that |
| 1888 | + the syntactic object is wrapped in a SyntacticTypeAnnotation (if necessary). |
| 1889 | + This ensures that a type annotation is obtained instead of e.g. a function. |
| 1890 | +
|
| 1891 | + Parameters |
| 1892 | + ---------- |
| 1893 | + syntactic_annotation : PyccelAstNode |
| 1894 | + A syntactic object that needs to be visited as a type annotation. |
| 1895 | +
|
| 1896 | + Returns |
| 1897 | + ------- |
| 1898 | + SyntacticTypeAnnotation |
| 1899 | + A syntactic object that will be recognised as a type annotation. |
| 1900 | + """ |
| 1901 | + if not isinstance(syntactic_annotation, SyntacticTypeAnnotation): |
| 1902 | + pyccel_stage.set_stage('syntactic') |
| 1903 | + syntactic_annotation = SyntacticTypeAnnotation(dtype=syntactic_annotation) |
| 1904 | + pyccel_stage.set_stage('semantic') |
| 1905 | + return syntactic_annotation |
| 1906 | + |
1883 | 1907 | def _get_indexed_type(self, base, args, expr):
|
1884 | 1908 | """
|
1885 | 1909 | Extract a type annotation from an IndexedElement.
|
@@ -1933,21 +1957,24 @@ def _get_indexed_type(self, base, args, expr):
|
1933 | 1957 | else:
|
1934 | 1958 | raise errors.report(f"Unknown annotation base {base}\n"+PYCCEL_RESTRICTION_TODO,
|
1935 | 1959 | severity='fatal', symbol=expr)
|
1936 |
| - if len(args) == 2 and args[1] is LiteralEllipsis() or len(args) == 1: |
1937 |
| - syntactic_annotation = args[0] |
1938 |
| - if not isinstance(syntactic_annotation, SyntacticTypeAnnotation): |
1939 |
| - pyccel_stage.set_stage('syntactic') |
1940 |
| - syntactic_annotation = SyntacticTypeAnnotation(dtype=syntactic_annotation) |
1941 |
| - pyccel_stage.set_stage('semantic') |
| 1960 | + if (len(args) == 2 and args[1] is LiteralEllipsis()) or len(args) == 1: |
| 1961 | + syntactic_annotation = self._convert_syntactic_object_to_type_annotation(args[0]) |
1942 | 1962 | internal_datatypes = self._visit(syntactic_annotation)
|
1943 |
| - type_annotations = [] |
1944 | 1963 | if dtype_cls in type_container :
|
1945 | 1964 | class_type = type_container[dtype_cls]
|
1946 | 1965 | else:
|
1947 | 1966 | raise errors.report(f"Unknown annotation base {base}\n"+PYCCEL_RESTRICTION_TODO,
|
1948 | 1967 | severity='fatal', symbol=expr)
|
1949 |
| - for u in internal_datatypes.type_list: |
1950 |
| - type_annotations.append(VariableTypeAnnotation(class_type(u.class_type), u.is_const)) |
| 1968 | + type_annotations = [VariableTypeAnnotation(class_type(u.class_type), u.is_const) |
| 1969 | + for u in internal_datatypes.type_list] |
| 1970 | + return UnionTypeAnnotation(*type_annotations) |
| 1971 | + elif len(args) == 2 and dtype_cls is PythonDictFunction: |
| 1972 | + syntactic_key_annotation = self._convert_syntactic_object_to_type_annotation(args[0]) |
| 1973 | + syntactic_val_annotation = self._convert_syntactic_object_to_type_annotation(args[1]) |
| 1974 | + key_types = self._visit(syntactic_key_annotation) |
| 1975 | + val_types = self._visit(syntactic_val_annotation) |
| 1976 | + type_annotations = [VariableTypeAnnotation(DictType(k.class_type, v.class_type)) \ |
| 1977 | + for k,v in zip(key_types.type_list, val_types.type_list)] |
1951 | 1978 | return UnionTypeAnnotation(*type_annotations)
|
1952 | 1979 | else:
|
1953 | 1980 | raise errors.report("Cannot handle non-homogenous type index\n"+PYCCEL_RESTRICTION_TODO,
|
|
0 commit comments