Title: Fix: Correct handling of `import module as alias` in LPython semantic analysis by amritamishra01 · Pull Request #2872 · lcompilers/lpython · GitHub
Open Graph Title: Fix: Correct handling of `import module as alias` in LPython semantic analysis by amritamishra01 · Pull Request #2872 · lcompilers/lpython
X Title: Fix: Correct handling of `import module as alias` in LPython semantic analysis by amritamishra01 · Pull Request #2872 · lcompilers/lpython
Description: Summary This PR fixes an issue where import statements with aliases (e.g., import numpy as np) would load the module correctly but fail to register the alias (np) in the current scope. As a result, subsequent code using the alias (like np.array()) would fail with NameError: 'np' is not defined. This patch updates the semantic analysis phase to ensure import aliases are correctly created and installed into the symbol table as ExternalSymbols. Technical Changes Modified visit_Import in src/lpython/semantics/python_ast_to_asr.cpp: Refactored the logic to merge module name extraction and module loading into a single loop. Previously, these were handled in separate loops, causing the alias information (x.m_names[i].m_asname) to be lost before the symbol registration step. Implemented Alias Registration: Added logic to check for the presence of an alias. If an alias exists, the compiler now creates an ASR::ExternalSymbol_t in the current scope. This symbol maps the alias name (e.g., np) to the loaded module symbol (e.g., numpy), mirroring Python's behavior. Verification I verified the fix using the following reproduction script repro.py: import numpy as np print(np.array([1, 2, 3])) Before Fix: Compiling failed with: semantic error: NameError: 'np' is not defined After Fix: The compiler successfully resolves the alias. Running with --show-asr confirms the creation of the ExternalSymbol: np: (ExternalSymbol 2 np 1 numpy numpy [] numpy Public ) Fixes #2814
Open Graph Description: Summary This PR fixes an issue where import statements with aliases (e.g., import numpy as np) would load the module correctly but fail to register the alias (np) in the current scope. As a result,...
X Description: Summary This PR fixes an issue where import statements with aliases (e.g., import numpy as np) would load the module correctly but fail to register the alias (np) in the current scope. As a result,...
Opengraph URL: https://github.com/lcompilers/lpython/pull/2872
X: @github
Domain: patch-diff.githubusercontent.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:52ddc961-596c-9509-9722-9ff4f8e643b6 |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | D848:F6370:44C998D:5E60021:6977F75C |
| html-safe-nonce | 7af110a77a0dd8412be29de45e209ef0c157e0c150c976b58f0541738678a4b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODQ4OkY2MzcwOjQ0Qzk5OEQ6NUU2MDAyMTo2OTc3Rjc1QyIsInZpc2l0b3JfaWQiOiIzNDk1NjY1NDQwMDc4MTY1ODUyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 317f24e5f9971a27c6fa81b066631111ff96e2cdf82fc7ea17ea9b632bf8375a |
| hovercard-subject-tag | pull_request:3078136980 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/lcompilers/lpython/pull/2872/checks |
| twitter:image | https://avatars.githubusercontent.com/u/171616930?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/171616930?s=400&v=4 |
| og:image:alt | Summary This PR fixes an issue where import statements with aliases (e.g., import numpy as np) would load the module correctly but fail to register the alias (np) in the current scope. As a result,... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9306bf4a8c8a88a5945f83e25033fc5e6fb7a05f40c5fca300ac67db7fee56ae |
| turbo-cache-control | no-cache |
| go-import | github.com/lcompilers/lpython git https://github.com/lcompilers/lpython.git |
| octolytics-dimension-user_id | 96538276 |
| octolytics-dimension-user_login | lcompilers |
| octolytics-dimension-repository_id | 442864346 |
| octolytics-dimension-repository_nwo | lcompilers/lpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 442864346 |
| octolytics-dimension-repository_network_root_nwo | lcompilers/lpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c586ac0f2edbf5b31f9ca03622220a4285e2def8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width