Skip to content

PYTHON-5488 append_metadata should not add duplicates #2461

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

sleepyStick
Copy link
Contributor

(not sure if this was urgent or not, but it was a relatively quick fix and i was worried this would be blocking the langchain PR)

Comment on lines 389 to 390
if driver.name in self.__metadata["driver"]["name"].split("|"):
return
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking we should be case-insensitive. "LangChain" vs. "langchain" should not make a difference.

I cannot remember if we already lowercase everything in name (I don't think we do).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call, changed!

@blink1073
Copy link
Member

Another thought, should we be adding a | if there is no version or platform, so that the fields always align?

@@ -386,6 +386,8 @@ def __init__(

def _update_metadata(self, driver: DriverInfo) -> None:
"""Updates the client's metadata"""
if driver.name.lower() in self.__metadata["driver"]["name"].lower().split("|"):
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to check for driver.name being None before calling lower().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the type of DriverInfo.name shouldn't be none. Would the check here be enforcing that the caller is following that rule?

Copy link
Contributor

@NoahStapp NoahStapp Aug 8, 2025

Choose a reason for hiding this comment

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

We check on line 393 that driver.name exists, so extending the same check here makes sense. We don't explicitly prevent driver.name from being None, so I'd prefer to err on the safe side to prevent errors being thrown by misbehaving users.

@@ -210,6 +210,46 @@ async def test_doesnt_update_established_connections(self):
self.assertIsNone(self.handshake_req)
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0)

async def test_duplicate_driver_name_no_op(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we refactor check_metadata_added to support checking that the new metadata is the same as the old metadata? Most of this method is just duplicate code from check_metadata_added.

@NoahStapp
Copy link
Contributor

Another thought, should we be adding a | if there is no version or platform, so that the fields always align?

Can you give an example?

@blink1073
Copy link
Member

If I add DriverInfo with no version, and then add one with version, how can we distinguish which name the version belongs to?

@NoahStapp
Copy link
Contributor

If I add DriverInfo with no version, and then add one with version, how can we distinguish which name the version belongs to?

Good point. Always adding "|" to each field, even if it's not present, seems like a good fix.

@ShaneHarvey
Copy link
Member

ShaneHarvey commented Aug 8, 2025

If I add DriverInfo with no version, and then add one with version, how can we distinguish which name the version belongs to?

Good catch. Sounds like we need a spec update for this case to ensure all drivers behave the same.
Edit: I see DRIVERS-3251 was already created.

@blink1073
Copy link
Member

@sleepyStick we're still missing the logic to ensure an equal number of | operators across the three strings.

@sleepyStick sleepyStick marked this pull request as ready for review August 11, 2025 16:25
@sleepyStick sleepyStick requested a review from a team as a code owner August 11, 2025 16:25
if driver.platform:
metadata["platform"] = "{}|{}".format(metadata["platform"], driver.platform)

name = driver.name if driver.name else ""
Copy link
Member

Choose a reason for hiding this comment

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

These can be simplified to driver.xxx or ""

@blink1073
Copy link
Member

drivers-pr-bot please backport to v4.14

Copy link
Member

@blink1073 blink1073 left a comment

Choose a reason for hiding this comment

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

LGTM!

@@ -386,20 +386,26 @@ def __init__(

def _update_metadata(self, driver: DriverInfo) -> None:
"""Updates the client's metadata"""
if driver.name and driver.name.lower() in self.__metadata["driver"]["name"].lower().split(
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor nit: we don't need the split() call here. Does having it improve readability?

Copy link
Contributor Author

@sleepyStick sleepyStick Aug 11, 2025

Choose a reason for hiding this comment

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

I was mainly worried about potential substrings? like if driver.name is a substring of an existing name, its not the same and therefore still should be added?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah good point! Great find on that edge case, better to be safe here and use the split!

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.

5 participants