class_docstr: A simple wrapper class for files which allows filedescriptors to be managed
  with normal Python ref counts.
  Without using this, if you create a file in a from_config you will see a warning
  along the lines of "ResourceWarning: self._file is acquired but not always released"
  this is because we're opening a file not in a context manager (with statement).
  We want to do it this way because it lets us pass a file object to the DataSource,
  rather than a filename. This exposes a ton more flexibility and testability, passing
  filenames is one of the paths towards pain.
  
  However, we don't have a clear resource management system set up for configuration.
  from_config functions are the tool that we have to allow objects to specify how they
  should be created from a configuration, which generally should only happen from the
  command line, whereas in eg. a notebook you should build the objects with
  constructors directly. If building from constructors, you can just open a file and
  pass it, but from_config here needs to create a file object from a configured
  filename. Python files don't close automatically, so you also need a system that
  will close them when the python interpreter shuts down. If you don't, it will print
  a resource warning at runtime, as the interpreter manually closes the filehandles
  (although modern OSs are pretty okay with having open file handles, it's hard for me
  to justify exactly why Python is so strict about this; I think one of the main
  reasons you might actually care is if you have a writeable file handle it might not
  have flushed properly when the C runtime exits, but Python doesn't actually
  distinguish between writeable and non-writeable file handles).
  
  This class is a wrapper that creates a system for (sort-of) safely closing the file
  handles before the runtime exits. It does this by closing the file when the object's
  deleter is called. Although the python standard doesn't actually make any guarantees
  about when deleters are called, CPython is reference counted and so as an
  mplementation detail will call a deleter whenever the last reference to it is
  removed, which generally will happen to all objects created during program execution
  as long as there aren't reference cycles (I don't actually know off-hand whether the
  cycle collection is run before shutdown, and anyway the cycles would have to include
  objects that the runtime itself maintains pointers to, which seems like you'd have
  to work hard to do and wouldn't do accidentally). This isn't true for other python
  systems like PyPy or Jython which use generational garbage collection and so don't
  actually always call destructors before the system shuts down, but again this is
  only really relevant for mutable files.
  
  An alternative implementation would be to build a resource management system into
  PyText, something like a function that we use for opening system resources that
  registers the resources and then we make sure are all closed before system shutdown.
  That would probably technically be the right solution, but I didn't really think of
  that first and also it's a bit longer to implement.
  
  If you are seeing resource warnings on your system, please file a github issue.
class_docstr_tok: ["A","simple","wrapper","class","for","files","which","allows","filedescriptors","to","be","managed","with","normal","Python","ref","counts",".","Without","using","this",",","if","you","create","a","file","in","a","from_config","you","will","see","a","warning","along","the","lines","of","``","ResourceWarning",":","self._file","is","acquired","but","not","always","released","''","this","is","because","we","'re","opening","a","file","not","in","a","context","manager","(","with","statement",")",".","We","want","to","do","it","this","way","because","it","lets","us","pass","a","file","object","to","the","DataSource",",","rather","than","a","filename",".","This","exposes","a","ton","more","flexibility","and","testability",",","passing","filenames","is","one","of","the","paths","towards","pain",".","However",",","we","do","n't","have","a","clear","resource","management","system","set","up","for","configuration",".","from_config","functions","are","the","tool","that","we","have","to","allow","objects","to","specify","how","they","should","be","created","from","a","configuration",",","which","generally","should","only","happen","from","the","command","line",",","whereas","in","eg",".","a","notebook","you","should","build","the","objects","with","constructors","directly",".","If","building","from","constructors",",","you","can","just","open","a","file","and","pass","it",",","but","from_config","here","needs","to","create","a","file","object","from","a","configured","filename",".","Python","files","do","n't","close","automatically",",","so","you","also","need","a","system","that","will","close","them","when","the","python","interpreter","shuts","down",".","If","you","do","n't",",","it","will","print","a","resource","warning","at","runtime",",","as","the","interpreter","manually","closes","the","filehandles","(","although","modern","OSs","are","pretty","okay","with","having","open","file","handles",",","it","'s","hard","for","me","to","justify","exactly","why","Python","is","so","strict","about","this",";","I","think","one","of","the","main","reasons","you","might","actually","care","is","if","you","have","a","writeable","file","handle","it","might","not","have","flushed","properly","when","the","C","runtime","exits",",","but","Python","does","n't","actually","distinguish","between","writeable","and","non-writeable","file","handles",")",".","This","class","is","a","wrapper","that","creates","a","system","for","(","sort-of",")","safely","closing","the","file","handles","before","the","runtime","exits",".","It","does","this","by","closing","the","file","when","the","object's","deleter","is","called",".","Although","the","python","standard","does","n't","actually","make","any","guarantees","about","when","deleters","are","called",",","CPython","is","reference","counted","and","so","as","an","mplementation","detail","will","call","a","deleter","whenever","the","last","reference","to","it","is","removed",",","which","generally","will","happen","to","all","objects","created","during","program","execution","as","long","as","there","are","n't","reference","cycles","(","I","do","n't","actually","know","off-hand","whether","the","cycle","collection","is","run","before","shutdown",",","and","anyway","the","cycles","would","have","to","include","objects","that","the","runtime","itself","maintains","pointers","to",",","which","seems","like","you","'d","have","to","work","hard","to","do","and","would","n't","do","accidentally",")",".","This","is","n't","true","for","other","python","systems","like","PyPy","or","Jython","which","use","generational","garbage","collection","and","so","don't","actually","always","call","destructors","before","the","system","shuts","down",",","but","again","this","is","only","really","relevant","for","mutable","files",".","An","alternative","implementation","would","be","to","build","a","resource","management","system","into","PyText",",","something","like","a","function","that","we","use","for","opening","system","resources","that","registers","the","resources","and","then","we","make","sure","are","all","closed","before","system","shutdown",".","That","would","probably","technically","be","the","right","solution",",","but","I","did","n't","really","think","of","that","first","and","also","it","'s","a","bit","longer","to","implement",".","If","you","are","seeing","resource","warnings","on","your","system",",","please","file","a","github","issue","."]
code: def __del__(self):
      self._file.close()
  
imports: import json
  import logging
  import re
  from typing import Any, Dict, List, Type, TypeVar
  from pytext.config.component import Component, ComponentType
  from pytext.data.utils import shard
  from pytext.utils.data import parse_slot_string, Slot
  from pytext.utils.file_io import PathManager
label_desc: Use image node_id 2 for calling the SafeFileWrapper obj's underlying member method code with example usage: obj.__del__() without return types