Skip to content

fix[lang]: disallow absolute relative imports#4268

Merged
charles-cooper merged 36 commits intovyperlang:masterfrom
sandbubbles:fix/relative-paths
Dec 17, 2024
Merged

fix[lang]: disallow absolute relative imports#4268
charles-cooper merged 36 commits intovyperlang:masterfrom
sandbubbles:fix/relative-paths

Conversation

@sandbubbles
Copy link
Copy Markdown
Contributor

@sandbubbles sandbubbles commented Oct 1, 2024

What I did

How I did it

  • Removed implicit relative imports in submodules, eliminating ambiguity in import paths.
  • Removed the lines that added the current directory to the top of the search paths. As a result, the poke_search_path function may no longer be necessary.

How to verify it

  • added tests

Commit message

this commit changes the behavior of search paths within
submodules. previously the current module's parent was appended to
the search path in the import recursion. this means that given the
following directory structure, this code is legal:

```
# directory structure:
# subdir/foo.vy
# subdir/bar.vy
```

```
# subdir/foo.vy
import bar
```

which has the same semantics as

```
# subdir/foo.vy
from . import bar
```

this represented a divergence wrt python's import system. in python,
only the second example is allowed.

this commit changes the semantics to match python, so that relative
imports use the current directory as search path, but absolute imports
can only use the top-level search path. (the directory containing the
top-level compilation target is still included in the search path, but
this only happens at the top of the recursion, same as in python -
https://docs.python.org/3/library/sys_path_init.html).

misc/refactor:
- remove dead `add_search_path()` method
- remove now-dead `poke_search_path()`

Description for the changelog

Cute Animal Picture

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

@sandbubbles sandbubbles changed the title fix[lang]: disallow absolute relative paths fix[lang]: disallow absolute relative imports Oct 1, 2024
Copy link
Copy Markdown
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.

yes, i think poke_search_paths is probably dead now. can you check? if so, let's remove it!

@@ -789,11 +789,7 @@ def _add_import(
# load an InterfaceT or ModuleInfo from an import.
# raises FileNotFoundError
def _load_import(self, node: vy_ast.VyperNode, level: int, module_str: str, alias: str) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i guess we can fuse load_import_helper right?

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 88.46%. Comparing base (ebe26a6) to head (be01034).
Report is 73 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4268      +/-   ##
==========================================
- Coverage   91.12%   88.46%   -2.67%     
==========================================
  Files         115      115              
  Lines       16235    16236       +1     
  Branches     2732     2733       +1     
==========================================
- Hits        14794    14363     -431     
- Misses       1004     1352     +348     
- Partials      437      521      +84     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cyberthirst
Copy link
Copy Markdown
Collaborator

cyberthirst commented Oct 17, 2024

@charles-cooper, have you considered the concept of packages? because we don't have it, we allow relative imports that can point even outside the project's directory

@charles-cooper
Copy link
Copy Markdown
Member

@sandbubbles please resolve merge conflicts

res = self.input_bundle.load_file(path)

if level != 0:
self.input_bundle.search_paths += [current_search_path]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
self.input_bundle.search_paths += [current_search_path]
self.input_bundle.search_paths.append(current_search_path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is kind of weird actually because on line 221, we already set search_paths to [current_search_path]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the issue is we need to set it temporarily to only one path, but then we need to add it to search paths in case there is an absolute import that uses it

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i pushed a clean up in a140226

Copy link
Copy Markdown
Contributor Author

@sandbubbles sandbubbles Dec 14, 2024

Choose a reason for hiding this comment

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

I must have though that example such as the following should work (but its what we were fixing in the first place).

#a.vy
from .subdir import b

and

#b.vy
from subdir.subsubdir import c

@cyberthirst cyberthirst added the release - must release blocker label Dec 11, 2024
@sandbubbles
Copy link
Copy Markdown
Contributor Author

This should be ready for rereview

@charles-cooper charles-cooper added this to the v0.4.1 milestone Dec 12, 2024
Copy link
Copy Markdown
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.

lgtm, thank you

@charles-cooper charles-cooper enabled auto-merge (squash) December 17, 2024 18:48
@charles-cooper charles-cooper merged commit b0ea1b3 into vyperlang:master Dec 17, 2024
pcaversaccio added a commit to pcaversaccio/snekmate that referenced this pull request Dec 18, 2024
### 🕓 Changelog

This commit updates the version `pragma`s in all Vyper source files to
target the latest `master` version `v0.4.1b4`, aligning with the release
of Vyper's newest beta version,
[`v0.4.1b3`](https://github.com/vyperlang/vyper/releases/tag/v0.4.1b3).
Additionally, this PR addresses a disallowed pattern in the
`IERC1155MetadataURI.vyi` interface definition by fixing the use of
absolute relative imports, which are prohibited in Vyper since PR
[#4268](vyperlang/vyper#4268).

---------

Signed-off-by: Pascal Marco Caversaccio <[email protected]>
willbrown84 added a commit to willbrown84/snekmate that referenced this pull request Sep 23, 2025
### 🕓 Changelog

This commit updates the version `pragma`s in all Vyper source files to
target the latest `master` version `v0.4.1b4`, aligning with the release
of Vyper's newest beta version,
[`v0.4.1b3`](https://github.com/vyperlang/vyper/releases/tag/v0.4.1b3).
Additionally, this PR addresses a disallowed pattern in the
`IERC1155MetadataURI.vyi` interface definition by fixing the use of
absolute relative imports, which are prohibited in Vyper since PR
[#4268](vyperlang/vyper#4268).

---------

Signed-off-by: Pascal Marco Caversaccio <[email protected]>
void-rider5560p added a commit to void-rider5560p/snekmate that referenced this pull request Sep 28, 2025
### 🕓 Changelog

This commit updates the version `pragma`s in all Vyper source files to
target the latest `master` version `v0.4.1b4`, aligning with the release
of Vyper's newest beta version,
[`v0.4.1b3`](https://github.com/vyperlang/vyper/releases/tag/v0.4.1b3).
Additionally, this PR addresses a disallowed pattern in the
`IERC1155MetadataURI.vyi` interface definition by fixing the use of
absolute relative imports, which are prohibited in Vyper since PR
[#4268](vyperlang/vyper#4268).

---------

Signed-off-by: Pascal Marco Caversaccio <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release - must release blocker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

import system allows absolute relative imports

3 participants