Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow functions to be named after builtins and reserved keywords #3307

Merged
merged 23 commits into from
Apr 8, 2023

Conversation

tserg
Copy link
Collaborator

@tserg tserg commented Mar 5, 2023

What I did

Since functions are in the self namespace, we can support function names that are identical to builtins and reserved keywords. This applies to both internal and external functions.

Also fixes #3227, and supercedes #3244.

How I did it

Split validate_identifier into validate_identifier_with_namespace (which is skipped with the skip_namespace_validation flag) and validate_identifier_name (which should always be run).

How to verify it

See test.

Commit message

feat: allow functions to be named after builtins and reserved keywords

Description for the changelog

Allow functions to be named after builtins and reserved keywords

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov-commenter
Copy link

codecov-commenter commented Mar 5, 2023

Codecov Report

Merging #3307 (7ae2aaf) into master (026f85f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##           master    #3307   +/-   ##
=======================================
  Coverage   88.95%   88.96%           
=======================================
  Files          84       84           
  Lines       10606    10605    -1     
  Branches     2214     2213    -1     
=======================================
  Hits         9435     9435           
+ Misses        767      766    -1     
  Partials      404      404           
Impacted Files Coverage Δ
vyper/utils.py 83.00% <ø> (-0.09%) ⬇️
vyper/codegen/stmt.py 89.38% <100.00%> (+0.08%) ⬆️
vyper/semantics/analysis/module.py 86.56% <100.00%> (ø)
vyper/semantics/namespace.py 97.22% <100.00%> (-0.15%) ⬇️
vyper/semantics/types/__init__.py 100.00% <100.00%> (ø)
vyper/semantics/types/base.py 91.53% <100.00%> (-0.07%) ⬇️
vyper/semantics/types/subscriptable.py 89.94% <100.00%> (ø)

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@tserg tserg changed the title fix: check internal function names for namespace collision feat: allow functions to be named after builtins and reserved keywords Apr 7, 2023
namespace = get_namespace()
if attr in namespace and attr not in [x for i in namespace._scopes for x in i]:
raise NamespaceCollision(f"Cannot assign to '{attr}', it is a builtin")
if attr.lower() in RESERVED_KEYWORDS or attr.upper() in OPCODES:
raise StructureException(f"'{attr}' is a reserved keyword")


def validate_identifier_name(attr):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can stay as validate_identifier

@@ -119,16 +120,23 @@ def override_global_namespace(ns):
_namespace = tmp


def validate_identifier(attr):
def validate_namespace_availability(attr):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change the arg name to ident?

if not re.match("^[_a-zA-Z][a-zA-Z0-9_]*$", attr):
raise StructureException(f"'{attr}' contains invalid character(s)")


# Reserved python keywords
PYTHON_KEYWORDS = set({"if", "for", "while", "pass", "def", "assert", "continue", "raise"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also does not need the set constructor!

@@ -421,22 +420,6 @@ def foo():
),
(
"""
struct X:
bar: int128
decimal: int128
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess these are allowed now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes they are, similar to def decimal(): pass

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is looking pretty good to me. my only question is if we should hold off until the 0.4.x series to merge, since it is kind of a breaking change (although in the sense that the compiler will accept more programs than before) to the language.

@charles-cooper
Copy link
Member

discussed with @fubuloubu offline, merging since it should not break existing programs

@charles-cooper charles-cooper merged commit e8b5b3c into vyperlang:master Apr 8, 2023
@tserg tserg deleted the fix/internal_collision branch April 8, 2023 16:02
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.

pop and append are not reserved keywords
4 participants