Fix HyponymDetector mutating module-level BASE_PATTERNS#577
Open
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Open
Fix HyponymDetector mutating module-level BASE_PATTERNS#577Chessing234 wants to merge 1 commit intoallenai:mainfrom
Chessing234 wants to merge 1 commit intoallenai:mainfrom
Conversation
self.patterns = BASE_PATTERNS binds self.patterns to the module-level list. When extended=True, self.patterns.extend(EXTENDED_PATTERNS) mutates BASE_PATTERNS itself, so: - every subsequent HyponymDetector (including extended=False) inherits the extended patterns, and - creating multiple extended detectors duplicates EXTENDED_PATTERNS in BASE_PATTERNS each time. Copy the list so self.patterns is independent of the module-level data.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
HyponymDetector.__init__withextended=Truemutates the module-levelBASE_PATTERNSlist in-place, so laterHyponymDetectorinstantiations pick up the extended patterns even whenextended=False, and repeatedextended=Trueconstructions duplicateEXTENDED_PATTERNSinsideBASE_PATTERNSeach time.https://github.com/allenai/scispacy/blob/eacccd4/scispacy/hyponym_detector.py#L41-L43
Root cause
self.patterns = BASE_PATTERNSbinds the attribute to the same list object imported fromscispacy.hearst_patterns. Callingself.patterns.extend(...)then mutates the module-level list rather than a private copy.Fix
Copy the list:
self.patterns = list(BASE_PATTERNS).BASE_PATTERNSis left untouched, each detector holds its own patterns list, and behavior is unchanged when only a single extended detector is created.