Skip to content

fix: stop HyponymDetector mutating the global BASE_PATTERNS#621

Open
Chessing234 wants to merge 1 commit into
allenai:mainfrom
Chessing234:fix/hyponym-detector-global-patterns
Open

fix: stop HyponymDetector mutating the global BASE_PATTERNS#621
Chessing234 wants to merge 1 commit into
allenai:mainfrom
Chessing234:fix/hyponym-detector-global-patterns

Conversation

@Chessing234

Copy link
Copy Markdown

Bug

HyponymDetector.__init__ does:

self.patterns = BASE_PATTERNS
if extended:
    self.patterns.extend(EXTENDED_PATTERNS)

self.patterns = BASE_PATTERNS aliases the module-global list, so .extend() mutates it in place. Every extended=True detector permanently appends EXTENDED_PATTERNS to the global BASE_PATTERNS (and duplicates them on each later instance); a subsequent extended=False detector then wrongly inherits the extended patterns.

Repro

base = len(BASE_PATTERNS)
HyponymDetector(nlp, extended=True); HyponymDetector(nlp, extended=True)
len(BASE_PATTERNS)  # before: base + 2*len(EXTENDED_PATTERNS); after: base

Fix

Copy the list before extending: self.patterns = list(BASE_PATTERNS).

Made with Cursor

`self.patterns = BASE_PATTERNS` aliased the module-level list, so
`self.patterns.extend(EXTENDED_PATTERNS)` mutated it in place. Each
extended detector permanently appended EXTENDED_PATTERNS to the global
list (duplicating on every later instance), leaking extended patterns
into subsequent non-extended detectors. Copy the list before extending.

Co-authored-by: Cursor <cursoragent@cursor.com>
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