René's URL Explorer Experiment


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

direct link

Domain: patch-diff.githubusercontent.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:bd865844-5da6-7328-9eea-d80d9a22b8ed
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idE786:32E2B7:80638:B6B3D:69787F04
html-safe-noncec1916a1dd9a48cf30dcb0fc750e29d35d94fc6ecb97e888ef177a8ea63d11508
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzg2OjMyRTJCNzo4MDYzODpCNkIzRDo2OTc4N0YwNCIsInZpc2l0b3JfaWQiOiI1MDg2MzQyNzYzMjUxMjY5MzgwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace79cd8ba9da9a98ca3052996efb1431ec6505a4328c6aea359cfe93c89fb0ba0
hovercard-subject-tagpull_request:3078136980
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/lcompilers/lpython/pull/2872/files
twitter:imagehttps://avatars.githubusercontent.com/u/171616930?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/171616930?s=400&v=4
og:image:altSummary 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_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None2981c597c945c1d90ac6fa355ce7929b2f413dfe7872ca5c435ee53a24a1de50
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/lcompilers/lpython git https://github.com/lcompilers/lpython.git
octolytics-dimension-user_id96538276
octolytics-dimension-user_loginlcompilers
octolytics-dimension-repository_id442864346
octolytics-dimension-repository_nwolcompilers/lpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id442864346
octolytics-dimension-repository_network_root_nwolcompilers/lpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release520b65a872113b919c1bbdb03834a50af15859fd
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#start-of-content
https://patch-diff.githubusercontent.com/
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Flcompilers%2Flpython%2Fpull%2F2872%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://patch-diff.githubusercontent.com/login?return_to=https%3A%2F%2Fgithub.com%2Flcompilers%2Flpython%2Fpull%2F2872%2Ffiles
Sign up https://patch-diff.githubusercontent.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=lcompilers%2Flpython
Reloadhttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Reloadhttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Reloadhttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
lcompilers https://patch-diff.githubusercontent.com/lcompilers
lpythonhttps://patch-diff.githubusercontent.com/lcompilers/lpython
Notifications https://patch-diff.githubusercontent.com/login?return_to=%2Flcompilers%2Flpython
Fork 173 https://patch-diff.githubusercontent.com/login?return_to=%2Flcompilers%2Flpython
Star 1.6k https://patch-diff.githubusercontent.com/login?return_to=%2Flcompilers%2Flpython
Code https://patch-diff.githubusercontent.com/lcompilers/lpython
Issues 503 https://patch-diff.githubusercontent.com/lcompilers/lpython/issues
Pull requests 85 https://patch-diff.githubusercontent.com/lcompilers/lpython/pulls
Actions https://patch-diff.githubusercontent.com/lcompilers/lpython/actions
Projects 0 https://patch-diff.githubusercontent.com/lcompilers/lpython/projects
Wiki https://patch-diff.githubusercontent.com/lcompilers/lpython/wiki
Security 0 https://patch-diff.githubusercontent.com/lcompilers/lpython/security
Insights https://patch-diff.githubusercontent.com/lcompilers/lpython/pulse
Code https://patch-diff.githubusercontent.com/lcompilers/lpython
Issues https://patch-diff.githubusercontent.com/lcompilers/lpython/issues
Pull requests https://patch-diff.githubusercontent.com/lcompilers/lpython/pulls
Actions https://patch-diff.githubusercontent.com/lcompilers/lpython/actions
Projects https://patch-diff.githubusercontent.com/lcompilers/lpython/projects
Wiki https://patch-diff.githubusercontent.com/lcompilers/lpython/wiki
Security https://patch-diff.githubusercontent.com/lcompilers/lpython/security
Insights https://patch-diff.githubusercontent.com/lcompilers/lpython/pulse
Sign up for GitHub https://patch-diff.githubusercontent.com/signup?return_to=%2Flcompilers%2Flpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://patch-diff.githubusercontent.com/login?return_to=%2Flcompilers%2Flpython%2Fissues%2Fnew%2Fchoose
amritamishra01https://patch-diff.githubusercontent.com/amritamishra01
lcompilers:mainhttps://patch-diff.githubusercontent.com/lcompilers/lpython/tree/main
amritamishra01:fix/import-aliashttps://patch-diff.githubusercontent.com/amritamishra01/lpython/tree/fix/import-alias
Conversation 2 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872
Commits 6 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits
Checks 18 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/checks
Files changed https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Please reload this pagehttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Fix: Correct handling of import module as alias in LPython semantic analysis https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#top
Show all changes 6 commits https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
424107c Fix: Support 'import module as alias' in semantic analysis amritamishra01 Dec 6, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/424107c10d959f07cd4da36e1ccc7786e6db9186
a802b36 Fix: Restore visit_ImportFrom and finalize aliasing logic amritamishra01 Dec 6, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/a802b363b76c7d0d2aefb3d9a53f1f38797209cb
90b2ce3 Fix: Restore visit_ImportFrom and remove double registration amritamishra01 Dec 6, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/90b2ce3444ea2f5240e2ad0592f8a1206274afd2
b4e6589 Fix: Restore visit_ImportFrom and finalize aliasing logic amritamishra01 Dec 6, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/b4e6589ff04fd7bd2b1b35b88b04d6ac67752690
78abfa6 Fix: Clean reset and correct alias patch amritamishra01 Dec 7, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/78abfa61a7d0576e21c8f4d4ba3a6ee1805a44b8
1a78800 Update libasr submodule pointer amritamishra01 Dec 11, 2025 https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/commits/1a78800309ae8301d9e27ead12fa02365ac45ed0
Clear filters https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Please reload this pagehttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
Please reload this pagehttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
libasr https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-2dc74531057e5720f8bf7ea87096c31d7ec3c98567da881e8a78a5b5f9027067
python_ast_to_asr.cpp https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-147457a9117ab6d023c02f9a359d6060c248c4216cfcb47b1fafd17d885063fe
libasrhttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-2dc74531057e5720f8bf7ea87096c31d7ec3c98567da881e8a78a5b5f9027067
Open in desktop https://desktop.github.com
libasrhttps://github.com/lfortran/lfortran
9 files https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005
.github/workflows/Exhaustive-Checks-CI.yml https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-41c4214de2b4aa65f804a1dfc1226b798946893aebb817d9c8d461c697f606d8
.github/workflows/Quick-Checks-CI.yml https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-b1928f78d564bc648a571e8b7ef76b3222ec6d2c7d7443c41cce5c43f93846a5
integration_tests/CMakeLists.txt https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-67c8fb3f4b6629c703ecb1c20661e195a435fe18141d370cd76cd2abc39021a8
integration_tests/c_ptr_06.f90 https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-59b99a43351d71e448fdccf4d5299f61025f82c2ddbee188267c4ac3c63d0370
integration_tests/file_34.f90 https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-4361e6cb7ffd75ac4fd0dca9797a24292ed1b2856c7b5a2ec5790723fc229a2c
integration_tests/intrinsics_390.f90 https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-ce8e467c48e3e67873d027738a82ccedbfbf529fd563c57b16b0895f4f6cf2dc
integration_tests/test_inquire_size.f90 https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-b70a295f7c8c90e2a8c5e8039287b31e212fd16da8d44406f9a5f0526b9da286
src/libasr/codegen/asr_to_llvm.cpp https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-48a7161397f9eb8b730f0cbec2f11ce1471aac961dc6b16dc558cf161c375054
src/libasr/runtime/lfortran_intrinsics.c https://patch-diff.githubusercontent.com/lfortran/lfortran/compare/9c7c52c21f847470c301cffd224f5cc2cf95fe63...f2a04fc2ae4dd9ccb940c0fee3f694791fec7005#diff-057d851a7170d57e545f3cbb89a1ef0e902dabd51532e5c25cb8d363b8c78725
src/lpython/semantics/python_ast_to_asr.cpphttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-147457a9117ab6d023c02f9a359d6060c248c4216cfcb47b1fafd17d885063fe
View file https://patch-diff.githubusercontent.com/lcompilers/lpython/blob/1a78800309ae8301d9e27ead12fa02365ac45ed0/src/lpython/semantics/python_ast_to_asr.cpp
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/{{ revealButtonHref }}
https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-147457a9117ab6d023c02f9a359d6060c248c4216cfcb47b1fafd17d885063fe
https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-147457a9117ab6d023c02f9a359d6060c248c4216cfcb47b1fafd17d885063fe
https://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files#diff-147457a9117ab6d023c02f9a359d6060c248c4216cfcb47b1fafd17d885063fe
Please reload this pagehttps://patch-diff.githubusercontent.com/lcompilers/lpython/pull/2872/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.