Allow callable (dynamic) values for Date and CalendarDate#1163
Open
gaoflow wants to merge 1 commit into
Open
Conversation
Date and CalendarDate subclass Number, whose Dynamic contract is that a parameter may be set to a callable that is invoked to produce the value on get. Number._validate_value honours this (callable(value) and not inspect.isgeneratorfunction(value) -> return), but the Date and CalendarDate overrides dropped the guard and went straight to the datetime type check, raising ValueError on a callable that the Number and Integer siblings accept. The dynamic machinery already resolves callable dates correctly on get; only validation rejected them. Add the same callable guard so all Dynamic siblings behave uniformly. Adds a parametrized regression test asserting Number/Integer/Date/CalendarDate all accept a callable and resolve it on get, via both assignment and constructor default.
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.
DateandCalendarDatesubclassNumber, and all of them ultimately subclassDynamic— whose contract is that a parameter may be set to a callable that is invoked to produce the value onget:Number._validate_valueexplicitly allows a callable (callable(value) and not inspect.isgeneratorfunction(value)), andIntegerinherits it — but theDateandCalendarDateoverrides drop that guard and go straight to the datetime type check, so they reject a callable that their sibling parameters accept.The dynamic machinery itself already handles callable dates correctly — with the guard added,
p.dresolves to the datetime and re-evaluates on each access like any otherDynamicvalue; only_validate_valuewas rejecting them.Fix. Add the same callable guard
Numberuses toDate._validate_valueandCalendarDate._validate_value, so allDynamicsiblings behave uniformly.Test.
TestDynamicCallableAcrossNumberSiblings(intests/testdynamicparams.py) parametrizes overNumber,Integer,Date,CalendarDateand asserts each accepts a callable and resolves it on get, both via assignment and via constructordefault=. Full suite (testdynamicparams,testdateparam,testcalendardateparam,testnumberparameter): 104 passed.