Skip to content

chore(generator): use global var for autopopulated fields#17890

Draft
hebaalazzeh wants to merge 1 commit into
mainfrom
global-variable-autopop-fields
Draft

chore(generator): use global var for autopopulated fields#17890
hebaalazzeh wants to merge 1 commit into
mainfrom
global-variable-autopop-fields

Conversation

@hebaalazzeh

Copy link
Copy Markdown
Contributor

Updating the use of global variable for auto populated fields

@hebaalazzeh hebaalazzeh self-assigned this Jul 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request refactors several Jinja2 templates to pre-calculate whether a service has auto-populated fields, replacing complex inline filters with a simpler boolean variable. The review feedback correctly points out that defining variables and executing loops before the {% extends %} tag violates Jinja2 standards, which can lead to syntax errors or scoping issues. It is recommended to move these variable definitions and loops inside the {% block content %} block to ensure correct rendering and compliance.

Comment on lines +1 to 10
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings and method_settings.auto_populated_fields %}
{% set has_auto_populated_fields.value = true %}
{% endif %}
{% endfor %}
{% extends '_base.py.j2' %}

{% block content %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Jinja2, the {% extends %} tag should be the first tag in the template. Defining variables and executing loops before {% extends %} can lead to syntax errors or unexpected rendering behavior in some Jinja2 environments. Additionally, variables defined outside of blocks in a child template are often not accessible inside the blocks. Moving the variable definition and loop inside the {% block content %} block ensures compliance with Jinja2 standards and guarantees correct scoping.

{% extends '_base.py.j2' %}

{% block content %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
  {% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
  {% if method_settings and method_settings.auto_populated_fields %}
    {% set has_auto_populated_fields.value = true %}
  {% endif %}
{% endfor %}

Comment on lines +1 to 10
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings and method_settings.auto_populated_fields %}
{% set has_auto_populated_fields.value = true %}
{% endif %}
{% endfor %}
{% extends "_base.py.j2" %}

{% block content %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Jinja2, the {% extends %} tag should be the first tag in the template. Defining variables and executing loops before {% extends %} can lead to syntax errors or unexpected rendering behavior in some Jinja2 environments. Additionally, variables defined outside of blocks in a child template are often not accessible inside the blocks. Moving the variable definition and loop inside the {% block content %} block ensures compliance with Jinja2 standards and guarantees correct scoping.

{% extends "_base.py.j2" %}

{% block content %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
  {% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
  {% if method_settings and method_settings.auto_populated_fields %}
    {% set has_auto_populated_fields.value = true %}
  {% endif %}
{% endfor %}

Comment on lines 1 to 12
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings and method_settings.auto_populated_fields %}
{% set has_auto_populated_fields.value = true %}
{% endif %}
{% endfor %}
{% extends '_base.py.j2' %}

{% block content %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Jinja2, the {% extends %} tag should be the first tag in the template. Defining variables and executing loops before {% extends %} can lead to syntax errors or unexpected rendering behavior in some Jinja2 environments. Additionally, variables defined outside of blocks in a child template are often not accessible inside the blocks. Moving the variable definition and loop inside the {% block content %} block ensures compliance with Jinja2 standards and guarantees correct scoping.

{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
{% extends '_base.py.j2' %}

{% block content %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
  {% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
  {% if method_settings and method_settings.auto_populated_fields %}
    {% set has_auto_populated_fields.value = true %}
  {% endif %}
{% endfor %}

Comment on lines 1 to 12
{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
{% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
{% if method_settings and method_settings.auto_populated_fields %}
{% set has_auto_populated_fields.value = true %}
{% endif %}
{% endfor %}
{% extends "_base.py.j2" %}

{% block content %}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

In Jinja2, the {% extends %} tag should be the first tag in the template. Defining variables and executing loops before {% extends %} can lead to syntax errors or unexpected rendering behavior in some Jinja2 environments. Additionally, variables defined outside of blocks in a child template are often not accessible inside the blocks. Moving the variable definition and loop inside the {% block content %} block ensures compliance with Jinja2 standards and guarantees correct scoping.

{# TODO(https://github.com/googleapis/gapic-generator-python/issues/2121): Remove the following variable (and the condition later in this file) for async rest transport once support for it is GA. #}
{% set rest_async_io_enabled = api.all_library_settings[api.naming.proto_package].python_settings.experimental_features.rest_async_io_enabled %}
{% extends "_base.py.j2" %}

{% block content %}
{% set has_auto_populated_fields = namespace(value=false) %}
{% for method in service.methods.values() %}
  {% set method_settings = api.all_method_settings.get(method.meta.address.proto) %}
  {% if method_settings and method_settings.auto_populated_fields %}
    {% set has_auto_populated_fields.value = true %}
  {% endif %}
{% endfor %}

@hebaalazzeh
hebaalazzeh force-pushed the global-variable-autopop-fields branch from 16f3b08 to f7a31e6 Compare July 24, 2026 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant