Skip to content

Access sections by both ID and name#531

Open
nforro wants to merge 1 commit intomainfrom
sections
Open

Access sections by both ID and name#531
nforro wants to merge 1 commit intomainfrom
sections

Conversation

@nforro
Copy link
Copy Markdown
Member

@nforro nforro commented Apr 8, 2026

Fixes #417.

Signed-off-by: Nikola Forró <nforro@redhat.com>
Assisted-by: Claude Opus 4.6 via Claude Code
@centosinfra-prod-github-app
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
Contributor

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

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 updates the Sections class to support lookups by both normalized ID and normalized name in the __contains__ and find methods. A new unit test has been added to verify this behavior. The review feedback suggests refining the __contains__ implementation to safely handle non-string inputs, such as Section objects, to avoid potential AttributeError exceptions when calling .lower().

Comment on lines +176 to +179
id_lower = cast(str, id).lower()
return any(
s.normalized_id == id_lower or s.normalized_name == id_lower for s in data
)
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

The __contains__ method currently assumes that the id argument is always a string and calls .lower() on it. Since Sections inherits from UserList[Section], it is expected that __contains__ can also handle Section objects (e.g., when checking section_obj in sections). If a non-string object is passed, this will raise an AttributeError because of the .lower() call.

It is recommended to check if id is a string first, and fall back to a standard list containment check otherwise.

Suggested change
id_lower = cast(str, id).lower()
return any(
s.normalized_id == id_lower or s.normalized_name == id_lower for s in data
)
if not isinstance(id, str):
return id in data
id_lower = id.lower()
return any(
s.normalized_id == id_lower or s.normalized_name == id_lower for s in data
)

@nforro nforro moved this from New to In review in Packit pull requests Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Whitespace after section header messes up with section namings

3 participants