code: def with_fragment(self, fragment):
          
          # N.B. doesn't cleanup query/fragment
          if fragment is None:
              fragment = ""
          elif not isinstance(fragment, str):
              raise TypeError("Invalid fragment type")
          return URL(
              self._val._replace(fragment=self._FRAGMENT_QUOTER(fragment)), encoded=True
          )
docs: Return a new URL with fragment replaced.
  
          Autoencode fragment if needed.
  
          Clear fragment to default if None is passed.