modular icon indicating copy to clipboard operation
modular copied to clipboard

[mojo-stdlib][docs] Update docs and code to use `from <module> import <entity>` instead of `from <module>.<file> import <entity>`

Open JoeLoser opened this issue 1 year ago • 10 comments

Review Mojo's priorities

What is your request?

Recently, we made a change so we public export all the entities in a given module's init.mojo file. So, it allows us to replace a lot of the code (both in doc comments at the top of a file within a module) and in modules themselves to use from <module_name> import <entity> rather than from <module_name.<specific_file> import <entity>.

What is your motivation for this change?

This is desirable so we become less coupled to the exact file a entity lives at within a module. In general, we should recommend this to other developers writing Mojo code too — even outside the standard library.

Any other details?

No response

JoeLoser avatar Mar 28 '24 19:03 JoeLoser

Hello, What for instances like from sys.info import * These kind of instances should remain same

FarhanAliRaza avatar Mar 28 '24 21:03 FarhanAliRaza

Should this PR include the code in test cases and examples also?

FarhanAliRaza avatar Mar 28 '24 21:03 FarhanAliRaza

What about cases like this from memory.unsafe import Reference, _LITRef Where Reference is exported but _LITRef is not exported. Should it be exported?

FarhanAliRaza avatar Mar 28 '24 21:03 FarhanAliRaza

Thanks for the eagerness to take this on! We would welcome this change throughout the repo - both source, test, and examples as it's a direction we'd like to go in.

Sorry I wasn't clear initially about the cases where we do from <some_module> import *. I think it's fine to leave those as-is for now. Our style guide says that we want to eliminate use of from <module> import * in general. However, I don't think we need to couple removal of that sort of thing with this issue. I'll file a separate issue to "import only the entities used" in lieu of import *.

For the _LITRef thing specifically, let's keep that particular one as is for right now if that works. That one specifically is subject to change a bit as the Reference type is starting to get more use in the standard library. I intentionally didn't export _LITRef in the memory/__init__.mojo file. That type isn't meant to be a user-facing type which is why it's prefixed with an underscore. There may be a few other types like this that are sort of internal compiler/standard library types related to Reference. If you see others, feel free to leave them as is, or just ping here, and I'll happily answer.

We very much welcome incremental progress on bigger things like this too 😄

JoeLoser avatar Mar 28 '24 23:03 JoeLoser

Should InlinedFixedVector be exported from collections?

FarhanAliRaza avatar Mar 29 '24 00:03 FarhanAliRaza

Hi Farhan, thanks for helping with this!

Should InlinedFixedVector be exported from collections?

I think it's reasonable that we keep InlinedFixedVector as exported from collections for the time being.

ConnorGray avatar Mar 29 '24 00:03 ConnorGray

it is not exported now

FarhanAliRaza avatar Mar 29 '24 00:03 FarhanAliRaza

it is not exported now

That appears to be an oversight — thanks for catching that! Feel free to open a PR that just changes this line to export InlinedFixedVector.

JoeLoser avatar Mar 29 '24 00:03 JoeLoser

https://github.com/modularml/mojo/pull/2047 Waiting for this to be merged so i can remove on import that is for InlinedFixedVector

FarhanAliRaza avatar Mar 29 '24 02:03 FarhanAliRaza

git fetch upstream git rebase upstream/nightly I ran this command. I am sorry wanted to use this PR https://github.com/modularml/mojo/pull/2047 to remove imports related to this. I am sorry if I caused any inconvenience. I am new to open source.

FarhanAliRaza avatar Mar 30 '24 08:03 FarhanAliRaza

Shouldn't it be closed since https://github.com/modular/modular/pull/2047 has been merged?

mmicu avatar Aug 01 '25 10:08 mmicu

Yeah, this got closed internally but not externally. Let's close this. Thanks!

JoeLoser avatar Aug 01 '25 13:08 JoeLoser