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

Setting namespace to prevent warning #97

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

pgoslatara
Copy link

When importing the parse_manifest function, a Pydantic warning is raised. This PR follows the Pydantic docs by overwriting the default protected namespace to prevent this warning (and the unsightly message it prints to console).

Replication:

➜  tmp python3.11 -m virtualenv .venv && source .venv/bin/activate
created virtual environment CPython3.11.6.final.0-64 in 69ms
  creator CPython3Posix(dest=/home/<REDACTED>/tmp/.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/<REDACTED>/.local/share/virtualenv)
    added seed packages: pip==24.0, setuptools==70.0.0, wheel==0.43.0
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
(.venv) ➜  tmp pip install dbt-artifacts-parser
...

Installing collected packages: typing-extensions, annotated-types, pydantic-core, pydantic, dbt-artifacts-parser
Successfully installed annotated-types-0.7.0 dbt-artifacts-parser-0.7.0 pydantic-2.8.2 pydantic-core-2.20.1 typing-extensions-4.12.2

[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: pip install --upgrade pip
(.venv) ➜  tmp python
Python 3.11.6 (main, Oct  2 2023, 13:45:54) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from dbt_artifacts_parser.parser import parse_manifest
/home/<REDACTED>/tmp/.venv/lib/python3.11/site-packages/pydantic/_internal/_fields.py:161: UserWarning: Field "model_unique_id" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
>>> 

The warning is also visible in previous runs of the test.yml workflow, example here.

@pgoslatara pgoslatara requested a review from yu-iskw as a code owner July 14, 2024 18:46
Copy link

coderabbitai bot commented Jul 14, 2024

Walkthrough

In several versions of the manifest parsers (manifest_v6.py, manifest_v7.py, manifest_v8.py, and manifest_v9.py), a new class-level attribute model_config of type ConfigDict with protected_namespaces set to an empty tuple has been introduced. This change standardizes the configuration setup within the ParsedMetric and Metric classes across different manifest versions.

Changes

File Change Summary
dbt_artifacts_parser/parsers/manifest/manifest_v6.py, dbt_artifacts_parser/parsers/manifest/manifest_v7.py Added model_config attribute in ParsedMetric class
dbt_artifacts_parser/parsers/manifest/manifest_v8.py, dbt_artifacts_parser/parsers/manifest/manifest_v9.py Added model_config attribute in Metric class

Poem

In the code where metrics thrive,
ConfigDicts now arrive.
Protected namespaces, none to see,
Making models run with glee.
Parsers v6 to v9 aligned,
With model_config, finely defined.
🐰✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bfd13e9 and c2041d8.

Files selected for processing (4)
  • dbt_artifacts_parser/parsers/manifest/manifest_v6.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v7.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v8.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v9.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • dbt_artifacts_parser/parsers/manifest/manifest_v6.py
Additional comments not posted (3)
dbt_artifacts_parser/parsers/manifest/manifest_v8.py (1)

1084-1085: LGTM!

The addition of model_config with protected_namespaces set to an empty tuple effectively addresses the Pydantic warning related to the protected namespace conflict.

dbt_artifacts_parser/parsers/manifest/manifest_v9.py (1)

1241-1242: LGTM!

The addition of model_config with protected_namespaces set to an empty tuple effectively addresses the Pydantic warning related to the protected namespace conflict.

dbt_artifacts_parser/parsers/manifest/manifest_v7.py (1)

1723-1724: LGTM! Addition of model_config aligns with PR objectives.

The addition of model_config with protected_namespaces set to an empty tuple prevents warnings related to the protected namespace "model_".

@pgoslatara
Copy link
Author

@yu-iskw Gentle nudge on this one. I'm building a tool on top of dbt-artifacts-parser and it's getting a little annoying getting warnings every time it's invoked.

@yu-iskw
Copy link
Owner

yu-iskw commented Aug 19, 2024

@pgoslatara Thank you for raising it. But, can you figure out if there is another approach to turn off the warnings, as I don't want to directly edit generated classes due to the maintainability. The discussion in #103 might be useful to understand the context behind it.

For instance, I tried to introduce base classes by the artifact type in order to control model configs. But, unfortunately, I don't have much time to delve into the approach at the moment.
#107

@pgoslatara
Copy link
Author

@yu-iskw Thanks for looking into this PR! Please note that I am rather new to Pydantic so my understanding/terminology may be incorrect.

But, can you figure out if there is another approach to turn off the warnings, as I don't want to directly edit generated classes due to the maintainability.

Yes! Another approach to silence the warning would be this:

import warnings
with warnings.catch_warnings():
    warnings.filterwarnings(
        "ignore",
        category=UserWarning
    )
    import dbt_artifacts_parser

My concern with this approach is that I have to apply it in every file where dbt_artifacts_parser is imported. And while this prevents the warning message, it only works for me. I'd like to solve this for everyone using this package, hence the approach in the PR where I tried to tackle the issue as specifically as possible while following Pydantic's advice at the same time.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c2041d8 and 3e53c7f.

Files selected for processing (4)
  • dbt_artifacts_parser/parsers/manifest/manifest_v6.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v7.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v8.py (1 hunks)
  • dbt_artifacts_parser/parsers/manifest/manifest_v9.py (1 hunks)
Additional comments not posted (4)
dbt_artifacts_parser/parsers/manifest/manifest_v8.py (1)

1085-1086: Approve addition of model_config with empty protected_namespaces.

The addition of model_config with protected_namespaces set to an empty tuple in the Metric class is a targeted solution to the namespace conflict warning. This change aligns with the PR objectives and follows Pydantic's recommendations.

However, consider adding a comment explaining this configuration to aid future maintainability.

+    # Setting protected_namespaces to an empty tuple to avoid namespace conflict warnings as per Pydantic's documentation
     model_config = ConfigDict(protected_namespaces=())
dbt_artifacts_parser/parsers/manifest/manifest_v9.py (1)

1242-1243: Approved: Namespace conflict resolution in the Metric class.

The addition of model_config = ConfigDict(protected_namespaces=()) effectively addresses the namespace conflict warning by setting protected namespaces to an empty tuple. This change aligns with the PR's objective and follows Pydantic's recommendations for handling such conflicts.

Ensure to verify that this change does not inadvertently affect other functionalities or interactions within the system that rely on the Metric class's configuration.

dbt_artifacts_parser/parsers/manifest/manifest_v6.py (1)

1610-1610: Approved change to protected_namespaces configuration.

The modification to set protected_namespaces to an empty tuple in the ParsedMetric class is a direct response to the namespace conflict warning described. This change should prevent the warning without altering the functionality of the parsing process.

The code change is approved.

It's recommended to verify that this configuration change does not affect other parts of the system that might rely on the default behavior of Pydantic. Consider adding tests or checks to ensure that no other functionalities are compromised.

Verification successful

Consistent Configuration Across Manifest Versions

The protected_namespaces configuration is consistently set to an empty tuple across multiple manifest versions (v6, v7, v8, v9). This indicates a deliberate strategy to handle namespace conflicts, and there is no evidence that other parts of the system rely on the default behavior of Pydantic's protected_namespaces. The change in manifest_v6.py is consistent with this approach and does not adversely affect other parts of the system.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that no other parts of the system are affected by the change in protected namespaces.

# Test: Search for any other references to `protected_namespaces` in the codebase. Expect: No other modifications related to this setting.
rg --type python "protected_namespaces"

Length of output: 70


Script:

#!/bin/bash
# Corrected script to search for any references to `protected_namespaces` in the codebase.
rg "protected_namespaces" --glob "*.py" -A 3

Length of output: 1293

dbt_artifacts_parser/parsers/manifest/manifest_v7.py (1)

1724-1725: Review: Setting protected_namespaces to an empty tuple in ParsedMetric.

The modification to set protected_namespaces to an empty tuple is intended to resolve a namespace conflict warning as described in the PR. This change follows the recommendation from Pydantic's documentation to handle such conflicts. It's crucial to ensure that this adjustment does not inadvertently affect other parts of the system that rely on the default namespace protections provided by Pydantic.

The change is approved as it directly addresses the issue described. However, it's recommended to monitor for any unintended side effects in areas of the codebase that interact with instances of ParsedMetric.

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.

2 participants