shivaam commented on code in PR #61350: URL: https://github.com/apache/airflow/pull/61350#discussion_r2755408050
########## scripts/ci/prek/check_airflow_imports_in_shared.py: ########## @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF 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. +# /// script +# requires-python = ">=3.10,<3.11" +# dependencies = [ +# "rich>=13.6.0", +# ] +# /// +from __future__ import annotations + +import argparse +import ast +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent.resolve())) +from common_prek_utils import console + + +def check_file_for_prohibited_imports(file_path: Path) -> list[tuple[int, str]]: + try: + source = file_path.read_text(encoding="utf-8") + tree = ast.parse(source, filename=str(file_path)) + except (OSError, UnicodeDecodeError, SyntaxError): + return [] Review Comment: I did some quick benchmarking locally just to see how ast.parse scales as we add more guardrails. While it’s definitely not a major issue today, the overhead is interesting to look at for the future: | Folders | Files | Lines | Parse (s) | I/O (s) | | :--- | :--- | :--- | :--- | :--- | | **Providers** | 1,990 | 337,628 | 0.59s | 0.05s | | **Airflow Core** | 619 | 104,664 | 0.21s | 0.02s | | **Other Support Tools** | 373 | 68,009 | 0.15s | 0.01s | | **Shared Libs** | 44 | 8,179 | 0.01s | 0.001s | ########## scripts/ci/prek/check_airflow_imports_in_shared.py: ########## @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF 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. +# /// script +# requires-python = ">=3.10,<3.11" +# dependencies = [ +# "rich>=13.6.0", +# ] +# /// +from __future__ import annotations + +import argparse +import ast +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).parent.resolve())) +from common_prek_utils import console + + +def check_file_for_prohibited_imports(file_path: Path) -> list[tuple[int, str]]: + try: + source = file_path.read_text(encoding="utf-8") + tree = ast.parse(source, filename=str(file_path)) + except (OSError, UnicodeDecodeError, SyntaxError): + return [] Review Comment: Nice addition! Just curious here. I noticed we use ast.parse in a few different hooks now. Do you think it’s worth eventually extracting a helper for that into common_prek_utils? It could help us centralize error handling and might make it easier to handle Python upgrades later. Do we have a sense of how much time these AST-based checks add to the CI pipeline? I'm wondering if running ast.parse in multiple separate steps might eventually add up. Just thinking out loud for the future! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
