Skip to content

Conversation

@chalmerlowe
Copy link
Collaborator

This PR introduces unit tests for the CodeAnalyzer's ability to extract class attributes.
These tests are located in test_generate_analyzer.py within the TestCodeAnalyzerAttributes
class.

Key Features of the New Tests:

  • Comprehensive Coverage: Includes tests for various ways attributes can be defined:
    • Class variables with and without type hints.
    • Instance variables with and without type hints, defined within __init__.
  • Parameterized Tests: Utilizes @pytest.mark.parametrize to efficiently test different
    code snippets and their expected structural output.
  • Clear Assertions: Directly compares the extracted structure with the expected
    structure using built-in assertion capabilities, after normalizing the attribute
    lists for order-independent comparison.

Why this change?

These tests ensure the CodeAnalyzer accurately parses Python code to identify class and
instance attributes, which is crucial for the microgenerator's functionality.

Broader Context:

The CodeAnalyzer and its tests are essential components supporting the development of a
microgenerator for the BigQuery Python SDK. This microgenerator is being created to
automatically generate a centralized BigQueryClient.

The primary goal of this centralized client is to ease the transition for our customers from
the traditional, largely handwritten BigQuery library to the newer GAPIC-generated version. It
aims to provide a unified interface, allowing users to interact with a single BigQueryClient
rather than multiple disparate *ServiceClient classes (e.g., JobServiceClient,
TableServiceClient, ModelServiceClient).

The CodeAnalyzer plays a critical role by parsing the existing *ServiceClient classes to
extract structural information (like methods and attributes). This information is then used by
the microgenerator to build the passthrough methods on the centralized BigQueryClient,
ensuring a user experience that remains familiar and convenient for our customers.

These new tests for CodeAnalyzer's attribute extraction ensure that we are correctly
understanding the structure of the underlying service clients, which is fundamental for the
microgenerator to generate a correct and useful BigQueryClient.

@chalmerlowe chalmerlowe requested review from a team as code owners October 6, 2025 15:44
@chalmerlowe chalmerlowe requested review from GaoleMeng and removed request for a team October 6, 2025 15:44
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: bigquery Issues related to the googleapis/python-bigquery API. labels Oct 6, 2025
Copy link
Contributor

@daniel-sanche daniel-sanche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with suggestions

"""
class MyClass:
CLASS_VAR = 123
""",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this would probably be more readable using parentheses for the multiline string, to avoid breaking indentation:

 pytest.param( ( "class MyClass:" " CLASS_VAR = 123", ), [ { "class_name": "MyClass", "methods": [], "attributes": [{"name": "CLASS_VAR", "type": None}], } ], id="class_var_assign", ), 
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might also be easier to read/scale if test cases are stored in a yaml file. We did something similar for bigtable here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. I will make a note of this. For now, the priority is to get a complete set of working unit tests, so not gonna jump on this refactor right away.

@chalmerlowe chalmerlowe merged commit 377b51c into autogen Oct 6, 2025
21 checks passed
@chalmerlowe chalmerlowe deleted the test/extraction-of-class-and-instance-attributes-Assign-and-AnnAssign branch October 6, 2025 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: bigquery Issues related to the googleapis/python-bigquery API. size: m Pull request size is medium.

4 participants