@@ -112,6 +112,8 @@ def magic_open(
112112
113113 if isinstance (path_or_stream , io .TextIOBase ):
114114 # mypy doesn't treat io.TextIOBase and typing.TextIO as the same thing
115+ # cast JUST tell's mypy "this is TextIO type" IT DOES NOT (repeat) DOES NOT
116+ # change the type of path_or_stream, just returns it
115117 yield cast (TextIO , path_or_stream )
116118 return
117119
@@ -121,6 +123,10 @@ def magic_open(
121123 raw = stack .enter_context (open (path_or_stream , "rb" ))
122124 raw_is_caller_owned = False
123125 else :
126+ # cast tells mypy that path_or_stream is a BinaryIO object
127+ # it DOES NOT (repeat) DOES NOT change the type of path_or_stream
128+ # it only is for type checking and doesn't modify path_or_stream at
129+ # run time, just returns it
124130 raw = cast (BinaryIO , path_or_stream )
125131 raw_is_caller_owned = True
126132
@@ -132,10 +138,17 @@ def magic_open(
132138 )
133139 header = bytes (header )
134140
141+ # If we can re-wind the stream to 0 after reading in the header
142+ # we do. This saves us from having to load the whole file into
143+ # memory. This branch doesn't affect the ownership of the source
135144 if raw .seekable ():
136145 raw .seek (0 )
137146 source = raw
138147 source_is_caller_owned = raw_is_caller_owned
148+ # If we can't re-wind the stream to 0, then we read in the whole
149+ # stream and concatenate it with the header we already read. This
150+ # changes the ownership of the stream to "us" so we want to make
151+ # sure that we close the stream when we are done
139152 else :
140153 source = io .BytesIO (header + raw .read ())
141154 source_is_caller_owned = False
0 commit comments