Skip to content

Conversation

@Wesseldr
Copy link

It looks like all manuals speak about mongo_conn_id, but the code uses conn_id. Hence is ignoring the mongo_conn_id that is set and falls back to the default connection. Hope this helps.


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

It looks like all manuals speak about mongo_conn_id, but the code uses conn_id. Hence is ignoring the mongo_conn_id that is set and falls back to the default connection. Hope this helps.
@boring-cyborg
Copy link

boring-cyborg bot commented Jan 14, 2023

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our pre-commits will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

hook_name = "MongoDB"

def __init__(self, conn_id: str = default_conn_name, *args, **kwargs) -> None:
def __init__(self, mongo_conn_id: str = default_conn_name, *args, **kwargs) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

This add breaking changes in case if user provide arguments as keyword:

MongoHook(conn_id="awesome-conn-id")

Copy link
Contributor

Choose a reason for hiding this comment

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

@Wesseldr Thanks for the contribution and your first PR! Definitely the docstring for the hook doesn't match what's actually required for use.

+1 on what @Taragolis mentioned.

We try to be very cognizant when thinking about backwards compat for users and try not to introduce breaking changes if avoidable. We really don't want users to upgrade a provider version and suddenly DAGs begin to break. In this situation, if the parameter name is changing, a DeprecationWarning should be raised that conn_id is deprecated and mongo_conn_id should be used. You can find a number of examples in other providers' hooks where a parameter is deprecated for reference on how to do this.

For the deprecation period, the hook should accept both conn_id and mongo_conn_id to maintain backwards compat and ultimately resolve to the self.mongo_conn_id value. Also the MongoSensor should ideally be updated to call this hook with keyword args.

The unit tests for the provider need to be updated accordingly too.

We're more than happy to help out if you are stuck. You can find us on Airflow Slack or we can communicate in this PR directly too.

hook_name = "MongoDB"

def __init__(self, conn_id: str = default_conn_name, *args, **kwargs) -> None:
def __init__(self, mongo_conn_id: str = default_conn_name, *args, **kwargs) -> None:
Copy link
Contributor

Choose a reason for hiding this comment

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

@Wesseldr Thanks for the contribution and your first PR! Definitely the docstring for the hook doesn't match what's actually required for use.

+1 on what @Taragolis mentioned.

We try to be very cognizant when thinking about backwards compat for users and try not to introduce breaking changes if avoidable. We really don't want users to upgrade a provider version and suddenly DAGs begin to break. In this situation, if the parameter name is changing, a DeprecationWarning should be raised that conn_id is deprecated and mongo_conn_id should be used. You can find a number of examples in other providers' hooks where a parameter is deprecated for reference on how to do this.

For the deprecation period, the hook should accept both conn_id and mongo_conn_id to maintain backwards compat and ultimately resolve to the self.mongo_conn_id value. Also the MongoSensor should ideally be updated to call this hook with keyword args.

The unit tests for the provider need to be updated accordingly too.

We're more than happy to help out if you are stuck. You can find us on Airflow Slack or we can communicate in this PR directly too.

@Wesseldr
Copy link
Author

Wesseldr commented Jan 17, 2023

@josh-fell @Taragolis Totally agree with avoiding code breaking changes..

There was no wisdom from my side here..

I just made the assumption the manual was written from the design and since that named it mongo_conn_id. Also an example I followed from MongoDB.com also used the parameter mongo_conn_id.

There for I posted a pull request for a name change as I started to believe that was the norm..
I hope you guys have an total all mighty ;-) overview about the whole providers naming conventions and can say what would be consistent naming for other providers as well.

Then either the manual is updated to represent conn_id instead of mongo_conn_id to make it all consistent again. (What was just slightly confusing until I read the code to find why the example wasn't working :)

Let me know what you pick, obviously dropping my PR is no problem if the current parameters are consistent with other providers and how the styleguide recommends these :-)
If needed I'll contact the author of the example@mongodb.com of the typo ;-)

Thank you for so quickly picking up on this and providing feedback&thoughts.
Let me know what you pick, and I'll follow along..

Resources
The manual I found about the Mongo-provider was here: airflow.providers.mongo.hooks.mongo v3.1.0

The example I followed that used the same Parameter mongo_conn_id naming was here: Using MongoDB with Apache Airflow - R Walters - 15Nov22

@Taragolis
Copy link
Contributor

The example I followed that used the same Parameter mongo_conn_id naming was here: Using MongoDB with Apache Airflow - R Walters - 15Nov22

Unfortunetly this is not Airflow documentation, and we can't change it easily.

I also agree with the fact that there is some inconsistency between Hook and Operators/Sensors.
Hook expected conn_id but Operators/Sensors mongo_conn_id

But there is no problem to resolve it, see some simple snippet:

import warnings

from airflow.hooks.base import BaseHook
from airflow.utils.types import NOTSET


class MongoHook(BaseHook):
    conn_id = "mongo_conn_id"
    default_conn_name = "mongo_default"
    conn_type = "mongo"
    hook_name = "MongoDB"

    def __init__(self, mongo_conn_id: str = NOTSET, *args, **kwargs) -> None:
        super().__init__()
        conn_id = kwargs.pop("conn_id", NOTSET)
        if conn_id is not NOTSET:
            warnings.warn(
                "Parameter `conn_id` is deprecated and will be removed "
                "in a future releases. Please use `mongo_conn_id` instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            if mongo_conn_id is NOTSET:
                mongo_conn_id = conn_id
            else:
                raise ValueError("You cannot provide both `mongo_conn_id` and `conn_id`.")
        elif mongo_conn_id is NOTSET:
            mongo_conn_id = self.default_conn_name

        self.mongo_conn_id = mongo_conn_id
        ...

    @property
    def conn_id(self):
        warnings.warn(
            "Property `conn_id` is deprecated and will be removed "
            "in a future releases. Please use `mongo_conn_id` instead.",
            DeprecationWarning,
            stacklevel=2,
        )
        return self.mongo_conn_id


hook1 = MongoHook("conn_id_as_attr")
assert hook1.conn_id == "conn_id_as_attr"
assert hook1.mongo_conn_id == "conn_id_as_attr"

hook2 = MongoHook(conn_id="conn_id_as_legacy_kwarg")
assert hook2.mongo_conn_id == "conn_id_as_legacy_kwarg"

hook3 = MongoHook(mongo_conn_id="conn_id_as_new_kwarg")
assert hook3.mongo_conn_id == "conn_id_as_new_kwarg"

try:
    MongoHook(mongo_conn_id="conn_id_as_new_kwarg", conn_id="conn_id_as_legacy_kwarg")
except ValueError:
    pass
else:
    raise ValueError("Unsupported")

hook_with_default_conn_id = MongoHook()
assert hook_with_default_conn_id.mongo_conn_id == MongoHook.default_conn_name

@potiuk
Copy link
Member

potiuk commented Jan 18, 2023

Yep. Changin it in back-compatible way as @Taragolis explained is a good idea.

@Taragolis
Copy link
Contributor

@Wesseldr, do you have a plan to continue work on this PR?

@github-actions
Copy link

github-actions bot commented Apr 5, 2023

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Apr 5, 2023
@github-actions github-actions bot closed this Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers pending-response stale Stale PRs per the .github/workflows/stale.yml policy file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants