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

Add type hints to schema modules #1771

Merged
merged 21 commits into from
May 5, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.next.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Thanks, you're awesome :-) -->

#### Improvements

* Add type hints to `schema` modules. #1771

#### Deprecated

<!-- All empty sections:
Expand Down
38 changes: 38 additions & 0 deletions scripts/_types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from .schema_fields import (
AllowedValues,
Field,
FieldDetails,
FieldEntry,
FieldNestedEntry,
MultiField,
Reuseable,
SchemaDetails,
)

__all__ = [
"AllowedValues",
"Field",
"FieldDetails",
"FieldEntry",
"FieldNestedEntry",
"MultiField",
"Reuseable",
"SchemaDetails",
]
97 changes: 97 additions & 0 deletions scripts/_types/schema_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import (
Any,
Dict,
List,
TypedDict,
)


class MultiField(TypedDict, total=False):
doc_values: bool
index: bool
ignore_above: int
name: str
norms: bool
type: str


class AllowedValues(TypedDict, total=False):
name: str
description: str


class Field(TypedDict, total=False):
allowed_values: List[AllowedValues]
dashed_name: str
description: str
doc_values: bool
example: str
flat_name: str
footnote: str
ignore_above: int
index: bool
intermediate: bool
level: str
multi_fields: List[MultiField]
name: str
node_name: str
normalize: List[str]
norms: bool
required: bool
short: str
type: str


class FieldDetails(TypedDict, total=False):
field_details: Field
fields: Dict[str, Field]


class Reuseable(TypedDict, total=False):
expected: List[Any]
top_level: bool
order: int


class SchemaDetails(TypedDict, total=False):
group: int
prefix: str
reusable: Reuseable
root: bool
title: str


class FieldEntry(TypedDict, total=False):
field_details: Field
fields: Dict[str, Field]
schema_details: SchemaDetails


class FieldNestedEntry(TypedDict, total=False):
description: str
fields: List[Field]
footnote: str
group: int
name: str
node_name: str
prefix: str
short: str
title: str
type: str
16 changes: 11 additions & 5 deletions scripts/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import argparse
import os
from typing import (
Optional,
)

from generators import asciidoc_fields
from generators import beats
Expand All @@ -30,12 +33,15 @@
from schema import finalizer
from schema import subset_filter
from schema import exclude_filter
from _types import (
FieldEntry
)


def main():
def main() -> None:
args = argument_parser()

ecs_generated_version = read_version(args.ref)
ecs_generated_version: str = read_version(args.ref)
print('Running generator. ECS version ' + ecs_generated_version)

# default location to save files
Expand All @@ -59,7 +65,7 @@ def main():
ecs_generated_version += "+exp"
print('Experimental ECS version ' + ecs_generated_version)

fields = loader.load_schemas(ref=args.ref, included_files=args.include)
fields: dict[str, FieldEntry] = loader.load_schemas(ref=args.ref, included_files=args.include)
cleaner.clean(fields, strict=args.strict)
finalizer.finalize(fields)
fields = subset_filter.filter(fields, args.subset, out_dir)
Expand All @@ -81,7 +87,7 @@ def main():
asciidoc_fields.generate(nested, ecs_generated_version, docs_dir)


def argument_parser():
def argument_parser() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--ref', action='store', help='Loads fields definitions from `./schemas` subdirectory from specified git reference. \
Note that "--include experimental/schemas" will also respect this git ref.')
Expand Down Expand Up @@ -111,7 +117,7 @@ def argument_parser():
return args


def read_version(ref=None):
def read_version(ref: Optional[str] = None) -> str:
if ref:
print('Loading schemas from git ref ' + ref)
tree = ecs_helpers.get_tree_by_ref(ref)
Expand Down
Loading