Raise clearer exception when using a yet undeclared variable in a type annotation #3215
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.
What I did
As per #3214, trying to reference a yet undeclared constant variable in the type annotation of another variable's declaration results in a fairly unhelpful error message:
vyper.exceptions.UndeclaredDefinition: 'self' has not been declared.
This fix ensures that a more detailed exception is raised.
How I did it
The exception is raised when trying to check whether the variable is a storage variable by checking if its name is in
self.namespace["self"].typ.members
.However this check can happen before
self
is added to the namespace. When the constant variable is referenced in a type annotation, the check will happen during folding and before validation, so there will be noself
key in namespace, leading to the previous exception being raised.A simple check on whether the key is present suffices to prevent this exception from being raised and to instead raise another detailed
UndeclaredDefinition
exception containing the undeclared variable's name and line number of the error.How to verify it
Compiling the following contract:
Now raises the following exception:
Commit message
Raise clearer exception when using a yet undeclared variable in a type annotation
Description for the changelog
Cute Animal Picture