OpenMetadata/ingestion/tests/unit/test_avro_parser.py
IceS2 e9c87c6adb
chore(ingestion): drop pylint, expand ruff (#27774)
* chore(ingestion): drop pylint, expand ruff to Stage 2c

Replace pylint with a coherent ruff-only stack (Stage 2c of the modernize
roadmap). Pylint is dropped from dev deps and CI workflows; ruff selected
ruleset expanded to ~22 families covering style, bug catchers, hygiene,
and the pylint port (PLE/PLC/PLW/PLR with the noisy "too-many-X"
complexity caps + magic-value disabled).

What's selected (with rationale in pyproject.toml):
  E, W, F, I, N         — style + correctness baseline + naming
  UP                    — pyupgrade (py>=3.10 modernizations)
  B, C4, C90, RET, SIM, TRY  — bug catchers
  PIE, ICN, T20, TC, TID, PTH, PERF  — hygiene
  PLE, PLC, PLW, PLR    — pylint port (PLR complexity caps ignored)
  RUF                   — ruff-native (incl. RUF100 unused-noqa)

What's removed:
  - .pylintrc (root) — duplicate of the ingestion pylint config
  - [tool.pylint.*] block in ingestion/pyproject.toml (~140 lines)
  - ingestion/plugins/{print_checker,import_checker}.py + tests + README
    (replaced by built-in T20 + TID251 banned-api respectively)
  - pylint dep from ingestion/setup.py and openmetadata-airflow-apis/pyproject.toml
  - `make lint` Makefile target + the pylint invocation in py_format_check
  - dead pylint TODO comment + ignored test entry in noxfile.py

Cwd-stable config: ruff is invoked both from the repo root (pre-commit,
CI) and from ingestion/ (`make py_format_check`). The `src`,
`extend-exclude`, and per-file-ignores entries are listed twice — once
relative to ingestion/ and once with the `ingestion/` prefix — so
first-party isort detection and exclusions match in both invocations.

Grandfathering: ran `ruff check --add-noqa` once + format-stable
iteration. ~12,130 noqa directives across ~1,400 files. Cleanup is
deferred to follow-up PRs that drop noqas one rule at a time.

Documentation sweep: replaced `make lint` references in CLAUDE.md,
AGENTS.md, DEVELOPER.md, copilot-instructions, and 6 SKILL files with
the apply+verify shape `make py_format && make py_format_check`.
`make py_format` is NOT a strict superset of pylint — it only applies
auto-fixable violations; `make py_format_check` catches the rest.

Basedpyright baseline regenerated: ruff format reflowed multi-line
signatures in ~70 files, shifting type-error column positions. The
basedpyright baseline matches by (file path, error code, range), so
column shifts caused 19 entries to mis-align. Net diff is small
(154 lines in/out of the 13MB baseline.json) — purely positional.

Verified locally:
  - make py_format_check         → All checks passed
  - nox --no-venv -s static-checks → 0 errors, 0 warnings, 0 notes

* chore(ingestion): finish ruff swap — nox lint session + skill docs

Three remaining stale-tooling references after Stage 2c:

  - `ingestion/noxfile.py` `lint` session was still calling `black --check`,
    `isort --check-only`, `pycln --diff`. Those tools aren't installed
    anywhere (we dropped them from dev deps). Replace with the ruff
    equivalents that mirror `make py_format_check`.
  - `skills/standards/code_style.md`: stack listed as `black + isort +
    pycln`; line length claimed 88 (black default). Both wrong: stack is
    ruff, line length is 120.
  - `skills/connector-building/SKILL.md`: `make py_format` comment said
    `# black + isort + pycln`. Same swap.

* chore(ingestion): keep main's baseline + globally ignore TRY400

Per gitar-bot's review on PR #27774:

1. Main's PR #27728 promoted ~60 `logger.warning()` → `logger.error()`
   inside `except` blocks. Those changes landed on main with their own
   baseline updates. Our PR doesn't promote anything — the merge from
   origin/main brought those `error` calls along with their baseline
   entries.

   The bot interpreted the `# noqa: TRY400` we added next to those lines
   as us silencing the rule case-by-case. Cleaner: globally ignore
   TRY400 in pyproject.toml, with a comment explaining why the codebase's
   `logger.error(...)` + separate `logger.debug(traceback.format_exc())`
   pattern is intentional. Strip ~430 per-line `# noqa: TRY400` markers
   from source.

2. Document that `S101` in `per-file-ignores` is a forward-looking
   entry — flake8-bandit (`S`) is not yet selected, so the rule is
   no-op today; the entry stays so when `S` lands later, tests don't
   immediately error.

Reverts the platform pin and Linux Docker–generated baseline. Keep
main's baseline intact and let CI surface the exact column-shifted
entries; the team will decide whether to fix in-place (revert format
on affected files) or add per-line `# pyright: ignore` markers.

* chore(ingestion): regen baseline for new connector type debt

Main's baseline was stale relative to recently-added connectors
(McpConnection, CustomDriveConnection) that lack common attributes
like `hostPort`, `database`, `catalog` etc. — all sites that access
those attributes via the union-typed `serviceConnection.root.config`
fire `reportAttributeAccessIssue` errors that aren't baselined.

71 errors + 58 warnings absorbed. Local macOS regen; pushing to see
CI's drift count. Per the basedpyright-baseline-and-ci PR experience,
macOS↔Linux column drift on this size of regen has historically been
1-7 residuals.
2026-04-28 07:21:59 +02:00

770 lines
19 KiB
Python

# Copyright 2025 Collate
# Licensed under the Collate Community License, Version 1.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE
# 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.
"""
Avro parser tests
"""
from unittest import TestCase
from metadata.parsers.avro_parser import parse_avro_schema
SAMPLE_AVRO_SCHEMA = """
{
"namespace": "openmetadata.kafka",
"name": "level",
"type": "record",
"doc": "This is a first level record",
"fields": [
{
"name": "uid",
"type": "int",
"doc": "The field represents unique id"
},
{
"name": "somefield",
"type": "string"
},
{
"name": "options",
"doc": "The field represents options array",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "lvl2_record",
"doc": "The field represents a level 2 record",
"fields": [
{
"name": "item1_lvl2",
"type": "string"
},
{
"name": "item2_lvl2",
"doc": "level 2 array",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "lvl3_record",
"fields": [
{
"name": "item1_lvl3",
"type": "string",
"doc": "The field represents level3 item"
},
{
"name": "item2_lvl3",
"type": "string"
}
]
}
}
}
]
}
}
}
]
}
"""
RECURSIVE_AVRO_SCHEMA = """
{
"name":"MainRecord",
"type":"record",
"fields":[
{
"default":"None",
"name":"NestedRecord",
"type":[
"null",
{
"fields":[
{
"default":"None",
"name":"FieldA",
"type":[
"null",
{
"items":{
"fields":[
{
"name":"FieldAA",
"type":"string"
},
{
"default":"None",
"name":"FieldBB",
"type":[
"null",
"string"
]
},
{
"default":"None",
"name":"FieldCC",
"type":[
"null",
"RecursionIssueRecord"
]
}
],
"name":"RecursionIssueRecord",
"type":"record"
},
"type":"array"
}
]
}
],
"name":"FieldInNestedRecord",
"type":"record"
}
]
}
]
}
"""
ARRAY_OF_STR = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "matrix",
"type": {
"type": "array",
"items": "string"
}
}
]
}
"""
ARRAY_OF_ARRAY = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "matrix",
"type": {
"type": "array",
"items": {
"type": "array",
"items": "int"
}
}
}
]
}
"""
ARRAY_OF_ARRAY_OF_RECORD = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "matrix",
"type": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "record",
"name": "Record",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
}
}
}
]
}
"""
ARRAY_OF_NESTED_ARRAY = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "matrix",
"type": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": "int"
}
}
}
}
}
}
]
}
"""
ARRAY_OF_NESTED_ARRAY_WITH_CHILD = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "matrix",
"type": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items": {
"type": "array",
"items":{
"type": "record",
"name": "Record",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
}
}
}
}
}
}
]
}
"""
UNION_EXAMPLE_1 = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "id",
"type": ["null","int"]
}
]
}
"""
UNION_EXAMPLE_2 = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "id",
"type": ["null","int","string","boolean"]
}
]
}
"""
UNION_EXAMPLE_3 = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "id",
"type": [
"null",
{
"type": "record",
"name": "Record",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
]
}
]
}
"""
UNION_EXAMPLE_4 = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "id",
"type": [
"null",
{
"type": "record",
"name": "Record",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
},
{
"type": "record",
"name": "Record2",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
]
}
]
}
""" # noqa: W293
UNION_OF_STR_AND_RECORD = """
{
"type": "record",
"name": "ArrayOfArrays",
"fields": [
{
"name": "id",
"type": [
"string",
{
"type": "record",
"name": "Record",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
]
}
]
}
"""
UNION_OF_ARRAY = """
{
"type": "record",
"name": "UnionOfArray",
"fields": [
{
"name": "matrix",
"type":[
"null",
{
"type": "array",
"items": "int"
}
]
}
]
}
"""
UNION_OF_ARRAY_OF_RECORD = """
{
"type": "record",
"name": "UnionOfArray",
"fields": [
{
"name": "matrix",
"type":[
"null",
{
"type": "array",
"items": {
"type": "record",
"name": "Record2",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
}
]
}
]
}
"""
UNION_OF_ARRAY_OF_RECORD_1 = """
{
"type": "record",
"name": "UnionOfArray",
"fields": [
{
"name": "matrix",
"type":[
"int",
{
"type": "array",
"items": {
"type": "record",
"name": "Record2",
"fields": [
{
"name": "field1",
"type": "string"
},
{
"name": "field2",
"type": "int"
}
]
}
}
]
}
]
}
"""
RECORD_INSIDE_RECORD = """
{
"type": "record",
"name": "OuterRecord",
"fields": [
{
"name": "id",
"type": "int"
},
{
"name": "name",
"type": "string"
},
{
"name": "innerRecord",
"type": {
"type": "record",
"name": "InnerRecord",
"fields": [
{
"name": "address",
"type": "string"
},
{
"name": "phoneNumbers",
"type": {
"type": "array",
"items": "string"
}
}
]
}
},
{
"name": "tags",
"type": {
"type": "array",
"items": "string"
}
}
]
}
"""
RECURSION_ISSUE_SAMPLE = """
{
"type": "record",
"name": "RecursionIssue",
"namespace": "com.issue.recursion",
"doc": "Schema with recursion issue",
"fields": [
{
"name": "issue",
"type": {
"type": "record",
"name": "Issue",
"doc": "Global Schema Name",
"fields": [
{
"name": "itemList",
"default": null,
"type": [
"null",
{
"type": "array",
"items": {
"type": "record",
"name": "Item",
"doc": "Item List - Array of Sub Schema",
"fields": [
{
"name": "itemList",
"type": [
"null",
{
"type": "array",
"items": "Item"
}
],
"default": null
}
]
}
}
]
}
]
}
}
]
}
"""
class AvroParserTests(TestCase):
"""
Check methods from avro_parser.py
"""
parsed_schema = parse_avro_schema(SAMPLE_AVRO_SCHEMA)
def test_first_level(self):
"""
Test nested schema
"""
self.assertEqual(self.parsed_schema[0].name.root, "level")
self.assertEqual(self.parsed_schema[0].description.root, "This is a first level record")
self.assertEqual(self.parsed_schema[0].dataType.name, "RECORD")
def test_second_level(self):
"""
Test nested schema
"""
children = self.parsed_schema[0].children
field_names = {str(field.name.root) for field in children}
self.assertEqual(
field_names,
{"uid", "somefield", "options"},
)
field_types = {str(field.dataType.name) for field in children}
self.assertEqual(field_types, {"INT", "STRING", "ARRAY"})
field_descriptions = {field.description.root if field.description else None for field in children}
self.assertEqual(
field_descriptions,
{
"The field represents unique id",
None,
"The field represents options array",
},
)
def test_third_level(self):
"""
Test nested schema
"""
level3_record = self.parsed_schema[0].children[2].children[0]
children = level3_record.children
self.assertEqual(level3_record.name.root, "lvl2_record")
self.assertEqual(level3_record.description.root, "The field represents a level 2 record")
self.assertEqual(level3_record.dataType.name, "RECORD")
field_names = {str(field.name.root) for field in children}
self.assertEqual(
field_names,
{"item1_lvl2", "item2_lvl2"},
)
field_types = {str(field.dataType.name) for field in children}
self.assertEqual(field_types, {"STRING", "ARRAY"})
field_descriptions = {field.description.root if field.description else None for field in children}
self.assertEqual(field_descriptions, {None, "level 2 array"})
def test_fourth_level(self):
"""
Test nested schema
"""
level3_record = self.parsed_schema[0].children[2].children[0]
children = level3_record.children[1].children[0].children
field_names = {str(field.name.root) for field in children}
self.assertEqual(
field_names,
{"item1_lvl3", "item2_lvl3"},
)
field_types = {str(field.dataType.name) for field in children}
self.assertEqual(field_types, {"STRING"})
def parse_schema_assert_without_child(self, schema: str, display: str):
example = parse_avro_schema(schema)
self.assertIsNotNone(example) # is parsed
field = example[0].children[0]
self.assertEqual(field.dataTypeDisplay, display)
self.assertIsNone(field.children) # no child
def parse_schema_assert_one_child(self, schema: str, display: str):
example = parse_avro_schema(schema)
self.assertIsNotNone(example) # is parsed
field = example[0].children[0]
self.assertEqual(field.dataTypeDisplay, display)
# has one child
self.assertIsNotNone(field.children)
self.assertEqual(len(field.children), 1)
def test_array_parsing(self):
"""
Test array parsing
"""
self.parse_schema_assert_without_child(ARRAY_OF_STR, "ARRAY<string>")
self.parse_schema_assert_without_child(ARRAY_OF_ARRAY, "ARRAY<ARRAY<int>>")
self.parse_schema_assert_without_child(ARRAY_OF_NESTED_ARRAY, "ARRAY<ARRAY<ARRAY<ARRAY<ARRAY<int>>>>>")
self.parse_schema_assert_one_child(ARRAY_OF_ARRAY_OF_RECORD, "ARRAY<ARRAY<record>>")
self.parse_schema_assert_one_child(
ARRAY_OF_NESTED_ARRAY_WITH_CHILD,
"ARRAY<ARRAY<ARRAY<ARRAY<ARRAY<record>>>>>",
)
def test_union_parsing(self):
"""
Test union parsing
"""
self.parse_schema_assert_without_child(UNION_EXAMPLE_1, "UNION<null,int>")
self.parse_schema_assert_without_child(UNION_EXAMPLE_2, "UNION<null,int,string,boolean>")
self.parse_schema_assert_one_child(UNION_EXAMPLE_3, "UNION<null,record>")
self.parse_schema_assert_without_child(UNION_EXAMPLE_4, "UNION<null,record,record>")
self.parse_schema_assert_without_child(UNION_OF_ARRAY, "UNION<null,ARRAY<int>>")
self.parse_schema_assert_without_child(UNION_OF_STR_AND_RECORD, "UNION<string,record>")
self.parse_schema_assert_one_child(UNION_OF_ARRAY_OF_RECORD, "UNION<null,ARRAY<record>>")
self.parse_schema_assert_without_child(UNION_OF_ARRAY_OF_RECORD_1, "UNION<int,array>")
def test_nested_record_parsing(self):
parsed_record_schema = parse_avro_schema(RECORD_INSIDE_RECORD)
# test 1st level record
self.assertEqual(parsed_record_schema[0].name.root, "OuterRecord")
self.assertEqual(parsed_record_schema[0].dataType.name, "RECORD")
# test 2nd level record
self.assertEqual(parsed_record_schema[0].children[2].name.root, "innerRecord")
self.assertEqual(parsed_record_schema[0].children[2].dataType.name, "RECORD")
# test fields inside 2nd level record
self.assertEqual(parsed_record_schema[0].children[2].children[0].name.root, "InnerRecord")
self.assertEqual(parsed_record_schema[0].children[2].children[0].dataType.name, "RECORD")
self.assertEqual(
parsed_record_schema[0].children[2].children[0].children[1].name.root,
"phoneNumbers",
)
self.assertEqual(
parsed_record_schema[0].children[2].children[0].children[1].dataType.name,
"ARRAY",
)
def test_recursive_record_parsing(self):
parsed_recursive_schema = parse_avro_schema(RECURSIVE_AVRO_SCHEMA)
# test that the recursive schema stops processing after 1st occurrence
self.assertEqual(
parsed_recursive_schema[0].children[0].children[0].children[0].children[0].name.root,
"RecursionIssueRecord",
)
self.assertEqual(
parsed_recursive_schema[0].children[0].children[0].children[0].children[0].children[2].name.root,
"FieldCC",
)
self.assertEqual(
parsed_recursive_schema[0]
.children[0]
.children[0]
.children[0]
.children[0]
.children[2]
.children[0]
.name.root,
"RecursionIssueRecord",
)
self.assertIsNone(
parsed_recursive_schema[0].children[0].children[0].children[0].children[0].children[2].children[0].children
)
def test_recursive_issue_parsing(self):
recur_parsed_schema = parse_avro_schema(RECURSION_ISSUE_SAMPLE)
self.assertEqual(
recur_parsed_schema[0].children[0].children[0].children[0].children[0].name.root,
"Item",
)
self.assertEqual(
recur_parsed_schema[0].children[0].children[0].children[0].name.root,
"itemList",
)
self.assertIsNone(
recur_parsed_schema[0].children[0].children[0].children[0].children[0].children[0].children[0].children
)